Sign in to follow this  

Drawing a Bitmap (MFC)

This topic is 4203 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
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

This topic is 4203 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this