Displaying a Bitmap
Ok, I have an MFC application with all my interface laid out. I have a CStatic control which I extended into the VideoProcessor class. I have a HBITMAP which I want to display. Now lets pretend, this HBITMAP is an image file (which in reality it isn't). In the OnPaint() function, I want to display this image.
I've spent 2 days trying to figure this out. I've read tutorial after tutorial, tried code chunk by code chunk. I can draw lines using the CPaintDC class but I can't BitBlt! I've tried to draw it on the parent form's paint event but with no luck either.
Here's my initialization:
Note: I've also tried SS_BITMAP instead of SS_BLACKFRAME.
videoBox.Create("", WS_CHILD | WS_VISIBLE | SS_BLACKFRAME, CRect(40, 40, 200, 160), this);
Please help, I can't believe I'm having so much trouble on such a trivial thing. No wonder people prefer C# or VB. But I *need* to draw this bitmap, asap! Thanks.
afx_msg void VideoProcessor::OnPaint()
HBITMAP bmp = ... ;
int width = 160, height = 120;
// draw bitmap here
You should try to understand the tutuorials/references, instead of copy-n-pasting chunks of code from them.
Here is the OnPaint() of the attached code:
// our paint DC
// load up our bitmap into bmME
// for resources use:
// for files use this:
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "avatar.bmp", IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hBmp == NULL)
// use our hidden static frame for bitmap placement
// create a memory DC
// select the bitmap into the memory dc
CBitmap *old = dcMem.SelectObject(&cbmME);
// get the raw bitmap for access to its attributes
// blit it
&dcMem, 0, 0, SRCCOPY);
Wow, thanks so much! I was reading tutorials but I couldn't figure out what was wrong. I think its because I was getting the DC of the screen or something. Thanks for the code! :D