Link to home
Start Free TrialLog in
Avatar of bolso
bolso

asked on

Bitmap files

How can i read each pixel of a Bitmap file, please give me some code?
Avatar of nietod
nietod

In what language?
Probably C++, right?  (I'm not totally stupid.  I thought this was the window section.)
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What I would do is load the bitmap from the file using LoadImage().  Then you don't have to "parse" the information in the file  (which can be done if you are interested in doing it that way.)

To load the image you would do.

HBITMAP ImgHnd = LoadImage(NULL,"c:\path\filename.bmp",IMAGE_BITMAP,0,0,LR_DEFAULTSIZE | LR_LOADFROMFILE);

This loads the image as a screen compatible bitmap.  I'm assuming that is what you want.  If you don't want it screen compatible it can be used to load a DIB bitmap.  

continues.
To read the pixels, you need to select the bitmap into a compatible device context.  To do that you would do

HDC DCHnd = CreateDC("DISPLAY",NULL,NULL,NULL);
HDC DCMem = CreateCompativleDC(DCHnd);

DeleteDC(DCHnd);

HBITMAP OldBitMap = SelectObject(DCMem,ImgHnd);// Select in image loaded from LoadImage().

// now use GetPixel() to get the pixels, like
COLOREF Color = GetPixel(DCMem,1,1);

DeleteDC(DCMem);

I hope this helps.  Ask if you have questions.
I have to go, but If you want to quickly get the pixel information in an array.  You can load the image as a DIB bitmap by specifying LR_CREATEDIBSECTION in the LoadImage() and then use GetDIBits() to get all the pixels at once.