Jump to content
  • Advertisement
Sign in to follow this  
gabzuka

Drawing a Bitmap (MFC)

This topic is 4515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 structure

BITMAPINFO 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 24
bminfo.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 image

int 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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!