none
Bitmap.LockBits and PixelFormat

    Question

  • Hi,

    To perform pixel-wise operations on a bitmap, you can call Bitmap.LockBits and cast the Scan0 to a pointer, and work with that pointer to modify the pixels of the bitmap.

    I wonder what happens if you call LockBits with a different PixelFormat than the one you've used to create the bitmap. Does it just calculate wrong offsets for the pixels, or does some kind of useful thing?

    Thanks,
    Gorkem PACACI
    Friday, June 27, 2008 2:03 PM

Answers

  • You must always use the BitmapData that LockBits() returns to determine how to manipulate your pointer.  As long as you pay attention to its Stride, Height and PixelFormat, you can't mess up.  Nothing useful happens when you intentionally do it wrong, other than some possible psychedelic effects or an AccessViolationException.
    Saturday, June 28, 2008 1:14 PM
    Moderator

All replies

  • Try it and report back.

    Friday, June 27, 2008 5:06 PM
  • You must always use the BitmapData that LockBits() returns to determine how to manipulate your pointer.  As long as you pay attention to its Stride, Height and PixelFormat, you can't mess up.  Nothing useful happens when you intentionally do it wrong, other than some possible psychedelic effects or an AccessViolationException.
    Saturday, June 28, 2008 1:14 PM
    Moderator