Resolved Looking for comment on LockBits

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
216
Programming Experience
10+
I think I have a bug in my program but the program appear to work ok.

I enter the code below with bmIn (from QuickWatch)
bmIn.PixelFormat Format32bppArgb {2498570} System.Drawing.Imaging.PixelFormat

Another time I enter with PixelFormat.Format24bppRgb

I QuickWatch p (see below for p) in both situations and see the same values.

1)Why no alpha byte in p when I enter with 32bpp?
It appears that LockBits is converting 32Bpp to 24bpp!

It would be great if I could count on it converting.

But MS LockBit doc says:

PixelFormat
A PixelFormat enumeration that specifies the data format of this Bitmap.
...
Exceptions
...
The incorrect PixelFormat is being passed in for the bitmap.
I'm giving it 32bpp and saying it's 24bpp.
2)Why no Exception?

VB.NET:
Dim bmDataIn As BitmapData
bmDataIn = bmIn.LockBits(New Rectangle(0, 0, bmIn.Width, bmIn.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
numBytes = bmDataIn.Stride * bmIn.Height

Dim p(numBytes - 1) As Byte
Marshal.Copy(bmDataIn.Scan0, p, 0, numBytes)
scanline = bmDataIn.Stride
For yIn As Integer = 0 To bmIn.Height - 1
    For xIn As Integer = 0 To bmIn.Width - 1 'Each xIn reads a color - assumed to be three bytes
        r = p(xIn * 3 + scanline * yIn + 2)
        g = p(xIn * 3 + scanline * yIn + 1)
        b = p(xIn * 3 + scanline * yIn)
        numR(r) += 1  'This records the number of times red has value r
        numG(g) += 1
        numB(b) += 1
        z += 1
      Next
   Next
 bmIn.UnlockBits(bmDataIn)
 
Solution
Back
Top