How Can I Paint a Pixel?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ofiras

    How Can I Paint a Pixel?

    Hi,
    I made a "Paint" program, but I couldn't find a method to paint 1
    pixel using graphic state ("Graphics g =
    Graphics.FromHw nd(this.Handle) ;")
    How can I paint 1 pixel?
    I guess I can make a Bitmap, change one pixel color and show it, but
    I'm sure there is another way of doing it.
    Please help,
    Ofir.
  • Pavel Minaev

    #2
    Re: How Can I Paint a Pixel?

    On Aug 5, 12:11 pm, ofiras <ofi...@gmail.c omwrote:
    Hi,
    I made a "Paint" program, but I couldn't find a method to paint 1
    pixel using graphic state ("Graphics g =
    Graphics.FromHw nd(this.Handle) ;")
    How can I paint 1 pixel?
    I guess I can make a Bitmap, change one pixel color and show it, but
    I'm sure there is another way of doing it.
    There isn't. GDI+ is a resolution-independent drawing framework, and
    the underlying device may not even be a raster device, in theory (GDI+
    plotter, anyone?). So painting a single-pixel bitmap is about the only
    workaround (there's no way to draw a 1-pixel-long line or rectangle
    reliably).

    Well, or you can use plain GDI via P/Invoke - that has SetPixel().

    Comment

    • ofiras

      #3
      Re: How Can I Paint a Pixel?

      On 5 אוגוסט, 10:24, Pavel Minaev <int...@gmail.c omwrote:
      On Aug 5, 12:11 pm, ofiras <ofi...@gmail.c omwrote:
      >
      Hi,
      I made a "Paint" program, but I couldn't find a method to paint 1
      pixel using graphic state ("Graphics g =
      Graphics.FromHw nd(this.Handle) ;")
      How can I paint 1 pixel?
      I guess I can make a Bitmap, change one pixel color and show it, but
      I'm sure there is another way of doing it.
      >
      There isn't. GDI+ is a resolution-independent drawing framework, and
      the underlying device may not even be a raster device, in theory (GDI+
      plotter, anyone?). So painting a single-pixel bitmap is about the only
      workaround (there's no way to draw a 1-pixel-long line or rectangle
      reliably).
      >
      Well, or you can use plain GDI via P/Invoke - that has SetPixel().
      Thanks... I'll use Bitmap to paint pixel...
      One more question - How can I save graphics that the user has drawn to
      Bitmap?
      Or how can I draw line/circle in Bitmap (so every time the user wii
      draw a line or a circle I can do it in the Bitmap too)?
      Please help,
      Ofir.

      Comment

      • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

        #4
        Re: How Can I Paint a Pixel?

        ofiras wrote:
        How can I save graphics that the user has drawn to
        Bitmap?
        Well, how do you keep the graphics so that you can repaint it on the screen?
        Or how can I draw line/circle in Bitmap (so every time the user wii
        draw a line or a circle I can do it in the Bitmap too)?
        You use the Graphics.FromIm age method to create a Graphics object that
        you can use to draw on the Bitmap.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Peter Bromberg[C# MVP]

          #5
          Re: How Can I Paint a Pixel?

          The System.Drawing. Bitmap class has a SetPixel and GetPixel method.


          "ofiras" <ofiasu@gmail.c omwrote in message
          news:e5b1bd81-9115-4849-b6b6-71b0636130ee@c5 8g2000hsc.googl egroups.com...
          Hi,
          I made a "Paint" program, but I couldn't find a method to paint 1
          pixel using graphic state ("Graphics g =
          Graphics.FromHw nd(this.Handle) ;")
          How can I paint 1 pixel?
          I guess I can make a Bitmap, change one pixel color and show it, but
          I'm sure there is another way of doing it.
          Please help,
          Ofir.

          Comment

          • ofiras

            #6
            Re: How Can I Paint a Pixel?

            On 5 אוגוסט, 11:30, Göran Andersson <gu....@guffa.c omwrote:
            ofiras wrote:
            How can I save graphics that the user has drawn to
            Bitmap?
            >
            Well, how do you keep the graphics so that you can repaint it on the screen?
            >
            Or how can I draw line/circle in Bitmap (so every time the user wii
            draw a line or a circle I can do it in the Bitmap too)?
            >
            You use the Graphics.FromIm age method to create a Graphics object that
            you can use to draw on the Bitmap.
            >
            --
            Göran Andersson
            _____http://www.guffa.com
            Thanks, it helped a lot.
            Another question - in bitmap, how can I find the nearest pixel (pixel
            1) to a specific pixel (pixel 2) that has different color from pixel
            2? Or how can I find a pixel in a specific distance from pixel 2 (like
            a circle that pixel 2 is his center, and I'm looking for a pixel that
            has different color that pixel 2)?
            (I'm trying to do a voronoi diagram maker, so I need to search for the
            nearest colored pixel in my bitmap for every pixel that is not colored
            (white))

            And one more question - how can I load picture and change its width
            and height?

            Comment

            • Peter Duniho

              #7
              Re: How Can I Paint a Pixel?

              On Tue, 05 Aug 2008 01:24:51 -0700, Pavel Minaev <int19h@gmail.c omwrote:
              [...]
              >How can I paint 1 pixel?
              >I guess I can make a Bitmap, change one pixel color and show it, but
              >I'm sure there is another way of doing it.
              >
              There isn't. GDI+ is a resolution-independent drawing framework, and
              the underlying device may not even be a raster device, in theory (GDI+
              plotter, anyone?). So painting a single-pixel bitmap is about the only
              workaround (there's no way to draw a 1-pixel-long line or rectangle
              reliably).
              Well, yes and no. Assuming an untransformed, pixel-units Graphics
              instance, Graphics.FillRe ctangle() would work fine. And assuming a
              _transformed_ Graphics instance, you shouldn't be worrying about reliably
              drawing exactly 1 pixel anyway (and under such a transformation, a
              single-pixel bitmap is going to be transformed as well).

              I don't see any real benefit to creating a whole Bitmap instance just to
              draw a pixel (and that's even if you do cache it).

              That said, I have a suspicion, based on the use of Graphics.FromHw nd(),
              that the OP has some more fundamental problems with respect to maintaining
              paint state.

              Pete

              Comment

              • Pavel Minaev

                #8
                Re: How Can I Paint a Pixel?

                On Aug 5, 8:49 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                wrote:
                Well, yes and no.  Assuming an untransformed, pixel-units Graphics  
                instance, Graphics.FillRe ctangle() would work fine.
                It didn't when I tried it. If you specify the size of the rectangle to
                draw as 1x1, then it actually paints a 2x2 pixel block. If you specify
                size as 0x0, it doesn't paint it at all. Or do you propose to feed it
                fractional values in hope that it will get the rounding correctly?

                Comment

                • Peter Duniho

                  #9
                  Re: How Can I Paint a Pixel?

                  On Tue, 05 Aug 2008 10:52:21 -0700, Pavel Minaev <int19h@gmail.c omwrote:
                  On Aug 5, 8:49 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                  wrote:
                  >Well, yes and no.  Assuming an untransformed, pixel-units Graphics  
                  >instance, Graphics.FillRe ctangle() would work fine.
                  >
                  It didn't when I tried it. If you specify the size of the rectangle to
                  draw as 1x1, then it actually paints a 2x2 pixel block. If you specify
                  size as 0x0, it doesn't paint it at all. Or do you propose to feed it
                  fractional values in hope that it will get the rounding correctly?
                  Do you have anti-aliasing turned on? If so, then that's a variation of a
                  "transforme d" graphics output and carries the same aspect that one should
                  not care about getting exactly a single pixel. If you are drawing to a
                  non-anti-aliased output, you should get one pixel.

                  Pete

                  Comment

                  Working...