Bitmap editing

closed account (jwkNwA7f)
How can I open a bitmap image and edit the pixels in it using WinAPI?

Thank you!


First load the bitmap with LoadImage:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045%28v=vs.85%29.aspx

This gives you an HBITMAP that contains the bitmap data.

Then you can access individual pixel data with GetDIBits and SetDIBits:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd144879%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162973%28v=vs.85%29.aspx


OR... if you don't mind doing a bit more setup... you can create a new bitmap with CreateDIBSection (too lazy to get a link... just search MSDN for it) that way you have a pointer to the raw pixel data and can change it at will.

Put the loaded bitmap into a DC, put the DIBSection bitmap into a DC, blit/copy the loaded bitmap to the DIBSection (look up BitBlt)... then you can modify it through that pointer.

Don't forget to clean up. Any bitmaps that you created/loaded need to be destroyed with DeleteObject, and any DCs you created need to be destroyed with DeleteDC.
closed account (jwkNwA7f)
Okay, thank you Disch!

Edit: My 400th post!
Last edited on
Topic archived. No new replies allowed.