0

I am trying to get all the pixels in an image using a Bitmap and GetPixels. Now I know that it is very inefficient so I have been looking into LockBits. I have successfully made what I think locks the bits but I can not get each and every pixels. My Code so far is...

//Creates Rectangle for holding picture
Rectangle bmpRec = new Rectangle(0, 0, bmp.Width, bmp.Height); 

BitmapData bmpData = bmp.LockBits(bmpRec, ImageLockMode.ReadWrite, bmp.PixelFormat); 

IntPtr Pointer = bmpData.Scan0; //Scans the first line of data

int DataBytes = Math.Abs(bmpData.Stride) * bmp.Height; //Gets array size

byte[] rgbValues = new byte[DataBytes]; //Creates array
string Pix = " ";
Marshal.Copy(Pointer, rgbValues, 0, DataBytes); //Copies of out memory

bmp.UnlockBits(bmpData);

for (int p = 0; p < DataBytes; p++)
{
     Pix += " " + rgbValues[p];
}

I would like to use Lockbits, as it is the best way to get pixels. Any help?

Thank you.

rguarascia.ts
  • 694
  • 7
  • 19
  • Not sure what you are asking... Just look at the rest of [LockBits](http://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx) sample you've copied to the post on how to get pixel values... – Alexei Levenkov May 05 '14 at 02:24
  • @AlexeiLevenkov I am having a problem getting every value of every pixel efficiently. Does my LockBits actually work correctly? and how do I get every pixel? Any help please? – rguarascia.ts May 05 '14 at 02:35
  • I don't see any attempts to get colors from the buffer - so hard to see where you have problem... Code looks ok but it is not possible to say for sure as `Pixels` is not defined. Start with simple format (16bpp or 32bpp) first as it is easier to handle pixels that are direclty mapping to `short` or `int`. – Alexei Levenkov May 05 '14 at 02:44
  • @AlexeiLevenkov Okay I can get the pixels now but when I use GetPixels I get different numbers than what I get with LockBits? It is my Pixel format that is incorrect? e.i. `bmp.GetPixels(i,j)` " Color [A=255, R=15, G=18, B=23]" `LockBits` "23 18 15"... Am I just doing everything wrong here. (I have updated my code for some more help. – rguarascia.ts May 05 '14 at 02:51
  • Looks fine. Most likely your bitmap is 24bpp (RGB, no A)... Pixel is always 32bpp (ARGB), with A defaulting to 255 if not used in the bitmap - so you just see RGB values. – Alexei Levenkov May 05 '14 at 02:54
  • @AlexeiLevenkov I think my problem now is that my pixels are displaying in the correct order... Also I have set my pixelformat to `PixelFormat Pixels = PixelFormat.Format32bppArgb;` and yet still my numbers aren't matching up? Is it my GetPixels that is incorrect? – rguarascia.ts May 05 '14 at 03:00
  • http://en.wikipedia.org/wiki/BMP_file_format 24bpp - "Specifically in the order (blue, green and red, 8-bits per each sample)" – Alexei Levenkov May 05 '14 at 03:03

1 Answers1

0

When accessing the data directly from LockBits, the storage order is B-G-R, not RGB... that is why you are getting the values in the reverse order.

Eric
  • 1