Saving a bitmap. [Archive] - C Board

PDA

View Full Version : Saving a bitmap.


jdinger
04-29-2002, 05:33 PM
How do I save a bitmap if all I know is the pixel data of an HDC
that I want to save it from?

What I want to do is 1) read all the pixel data from the 1st HDC
(which has the original bitmap loaded into it). Then 2) blit those
pixels into another HDC with modifications. Lastly 3) save the
image that was blitted into the 2nd HDC as a bitmap.

I know how to do steps 1 and 2 with GetPixel and SetPixel, I just
don't know how to save the contents of the 2nd HDC into
a bitmap file.

I know the structure of a bitmap file and I know the file will be a
16-bit bitmap.

All I can think of would be if I have to define my own
BITMAPFILEHEADER and BITMAPINFOHEADER, populate an array
of COLORREFs (that would hold pixel data). Then use CreateFile
and WriteFile to make the bitmap. I was just wondering (hoping)
if there was a function that made it easier/less combersome than
that.

Thanks in advance for any help.

jdinger
04-29-2002, 09:05 PM
Just wanted to point out, no big rush on this. I've got a temp fix
using VB and the SavePicture function, but I'd really like to do it in
C++.

no-one
04-29-2002, 11:35 PM
well i would recommend writing a loader/saver for yourself! i did, and it did me good

a really good doc on the full BMP format is here it even explains RLE compression ect...
http://www.wotsit.org/download.asp?f=bmpfrmat

novacain
04-30-2002, 08:19 AM
I only use jpg's so I'm not sure if this will work.

Could you use SelectObject() to 'push' out the bitmap from the HDC?
CreateCompatibleBitmap() for the new bmp and use BitBlt() if you need a copy.
GetObject() to find its properties (fill a BITMAP struct from the HBITMAP provided)
Use GetDIBits() to create the BITMAPINFO struct that can be saved.

jdinger
05-01-2002, 01:40 PM
Could you use SelectObject() to 'push' out the bitmap from the HDC?
CreateCompatibleBitmap() for the new bmp and use BitBlt() if you need a copy.
GetObject() to find its properties (fill a BITMAP struct from the HBITMAP provided)
Use GetDIBits() to create the BITMAPINFO struct that can be saved.

Thanks, novacain. I tried it your way and (including error checking)
I got it down to about 50 lines of code.

Back in my VB days ( the dark ages... :( ) I used a function called
SavePicture which took 2 params: the image to copy from and the
filename to save it as.

What I did now was write a my own C++ function that takes
2 params: the hdc of that holds the image and the filename to
save it to.

So I got the simple 1 line function to save my bitmaps that I was
accustomed to and I learned a ton by writing it. :D