BITMAP creation - a little problem I have

Well, me again.
I have continued with my little project and I have this little problem. I get some bytes from a BMP image and I transform them into a HBITMAL, which I display into a STATIC control.

My projects was uploaded here: http://www.devimperium.info/CreateBitmap.zip

The problem is that the image is displayed but it is moved to the right a little bit and that space is filled with some weird color (it seems to be the negative of some portion of the image).

Screen capture here: http://www.devimperium.info/screen8.jpg

Don't know what could be the problem.
Thanks!
Last edited on
WTF - unless the whole aim of the exercise is to do it the long way around like you have done - then you do it the easy way in TWO lines of code.

P.S - I have to ask - how may projects are you doing at the same time.
Last edited on
Well, I am working on many project at the same time. Even so, they share the main code (the creation of the window and common functions). Each one has a purpose and depending on it the project may contain a new part. I don't know when all this will end, it's like a chain reaction. When you find out the solution for a problem you want to try something more difficult. C++ is like an ugly girl with a lovely soul.

Anyway, back to my stuff.
I know that I just could load the image but it is not about that. I am planning to create a bigger file that contains more images stored, one by one, like a cache file. Firstly, I want to see if I can get the bytes of an image into a HBITAMP. If it works I will join three images into a single file, I will save their starts and counts into another file, then I will get their bytes and display them.
Last edited on
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
So, I have two possibilities of doing this"
1.) Send only the bytes that specifies the pixels of the image, removing the header information
2.) Don't send the header information as an argument to the function. Send only the bytes of the image, then find out(inside the function) which is the size, the bytes/pixel and finally, the bytes of the image.

Thanks. I thought that CreateDIBSection was getting only the pixels of the image (not the header)
Topic archived. No new replies allowed.