1 2 3 4 5 6 7 8 9 10
|
if ((fp = fopen((char*)dest.c_str(), "rb")) != NULL) {
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
bytes = (char*)malloc(len+1);
fread(bytes, len, 1, fp);
fclose(fp);
bytes[len] = 0;
}
HBITMAP bitmap = CreateBitmapFromPixels(hdc, 790, 500, 24, bytes);
|
You have a few problems / oddities.
You don't need to null terminate bitmaps. They're not char arrays.
Bitmaps contain information other than the pixel data. Things like width, height, bpp, etc are in the header, along with [possibly] palette information. You are taking all of that and treating it all as if it were pixel data, which will result in some garbage pixels and misalignment (which is what you are seeing in your output).
If you really want to work with binary bitmap files, you'll have to read the file format. A google search will turn up the format. Here's one I found that is very simple:
http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
It helps if you open a bitmap in a hex editor and see the raw data so you can understand how it works. Whenever working with binary files, I recommend viewing in a hex editor. If you don't have one, get one, there are free ones everywhere.
Here is what [the start of] your "It Works" image.bmp file looks like in a hex editor:
http://i40.tinypic.com/34rxf1g.png
Look at that and follow along with the above linked format spec.
It starts with 42 4D (which is 'BM' to indicate it's a bitmap file.)
Followed by 06 19 12 00 -- which is 0x00121906, which is the size of the file
Followed by 00 00 00 00 -- which are reserved bytes
followed by 36 00 00 00 -- which is 0x00000036, which is the location in the file that is the actual start of the pixel data.
followed by 28 00 00 00 -- 0x00000028, the number of bytes in the upcoming header (the header containing dims/other details)
16 03 00 00 = 0x00000316 = 790 ,the width of the image in pixels
... etc
... etc