Skip to main content
gamedev.net
🔒 Locked

Drawing a Bitmap (MFC)

Started by gabzuka Jun 13, 2006 at 11:19 AM 2 replies 1.8k views
Original Post
gabzuka
gabzuka
Hi gamedevPeople ... I'm very new to MFC and I'm making an aplication that displays some images (.bmp, .jpg, ...) and makes some changes to them (brightenss, contrast, grayscale, etc ...) For this I used the FreeImage library and converted my image file into a BYTE array. To display the image I'm using a couple of for()'s to go through the array and using SetPixel() I display every pixel ... as I understand this is not the fastest way to do it but all the informtaion on the internet is overwhelming (DIBs, BitBlt, HBITMAP, CBitmap, etc etc etc), and can´t seem to understand how to do it ... Could any of you help me out, or at least point me in the right direction? Thanks a lot in advance, G
Emmanuel Deloget
Emmanuel Deloget
The simplest possible way is probably to use StretchDIBits(). To be able to use it, you must create a valid BITMAPINFO structure:

// ===== create the BITMAPINFO structureBITMAPINFO bminfo;memset(&bminfo, 0, sizeof(bminfo));bminfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bminfo.bmiHeader.biWidth = your_width;bminfo.bmiHeader.biHeight = your_height;bminfo.bmiHeader.biPlanes = 1;bminfo.bmiHeader.biBitCount = your_bit_count; // probably 24bminfo.bmiHeader.biCompression = BI_RGB;// this is mega important: the pitch of an image is the distance (in bytes)// between the first pixel of the first line and the first pixel of the // second line. THE NUMBER OF PIXELS IN A LINE SHOULD BE A MULTIPLE OF 4// (even if your_width is not a multiple of 4). You must correctly setup // your image before displaying it.int pitch = ((your_width+3)&(~3)) * your_bit_count / 8;bminfo.bmiHeader.biSizeImage = your_height * pitch;// ===== display the imageint error = StretchDIBits(hdc,                           0, 0, wnd_width, wnd_height, // dest                          0, 0, your_width, your_height, // source                          your_imgae_bits,                          &bminfo,                          DIB_RGB_COLORS,                          SRC_COPY);if (error == GDI_ERROR) {  // somethwing weird happened}


HTH,
shadowcomplex
shadowcomplex
I have written an MFC specific class to handle Bitmaps and draw them in various fashions (regular blit, transparent blit, and alpha blending). It simplifies the code to something like this:

CEBitmap Bmap;

Bmap.LoadBitmap(IDB_PICTURE);

Bmap.Paste(NULL,Xcoord,Ycoord,mainCDC);
Bmap.PasteTransparent(NULL,Xcoord,Ycoord,RGB(255,0,255),mainCDC);
Bmap.PasteAlpha(NULL,Xcoord,Ycoord,Alpha,mainCDC);


Where IDB_PICTURE is defined and associated with a specific file.
If you are interested I can send you the CEBitmap.h and .cpp files.

gabzuka
gabzuka
Wow! Emmanuel that was great! it worked! thanks a lot!! that was what I was looking for ...

shadowcomplex could you send me that code anyway, it looks interesting, I would really apreciate it ... (gabriel at rubberband.com.ve) ...

Thanks a lot both of you again ...

G

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.