|
-
July 6th, 2012, 01:09 PM
#5
Re: How to get an file-loaded bitmap to fit a static image container using StretchBlt
Sounds easy. But it's not. Here's my attempt. It crashes the app apparently due to heap corruption related to byte counts. Inspection of my code will reveal that I am confused about how to create compatible device contexts (DC), what dimensions to use, how to get the dimensions of the newly loaded bitmap, how to create a thumbnail bitmap (rather than just the handle ?), how to 'deselect' an object, how to 'destroy' a DC, and just about everything else.
Code:
// Load the Image File
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, m_csPathname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
// create a compatible memory context
HDC memDCdst;
CDC * pDC = m_cImage.GetDC();
memDCdst = CreateCompatibleDC(pDC->m_hDC);
// create compatible thumbnail bitmap of the size of your static control
CRect rect;
m_cImage.GetWindowRect(rect);
ScreenToClient(rect);
int bmpWidth = rect.Width();
int bmpHeight = rect.Height();
HBITMAP hBmpDst = CreateCompatibleBitmap(memDCdst, bmpWidth, bmpHeight);
// select the thumbnail bitmap into Dst
SelectObject(memDCdst, hBmpDst);
// create compatible memory context Src
HDC memDCsrc;
memDCsrc = CreateCompatibleDC(memDCdst); // use the destination DC (or what ??)
// select your previously loaded image bitmap into Src
SelectObject(memDCsrc, hBmp);
// pre-calculate Dst target rectangle (optional, 2) - done above
int xDest = rect.TopLeft().x;
int yDest = rect.TopLeft().y;
int wDest = rect.Width();
int hDest = rect.Height();
// do StrechBlt to Dst from Src
StretchBlt(memDCdst, xDest, yDest, wDest, hDest, memDCsrc, 0, 0, bmpWidth, bmpHeight, SRCCOPY);
// unselect the loaded bitmap from Src
// The SelectObject function selects an object into the specified device context (DC).
// The new object replaces the previous object of the same type.
SelectObject(memDCsrc, hBmpDst);
// unselect the thumbnail bitmap from Dst
SelectObject(memDCdst, hBmpDst);
// destroy Dst context
delete memDCdst;
memDCdst = NULL;
// destroy Src context
delete memDCsrc;
memDCsrc = NULL;
// set thumbnail bitmap to static control
m_cImage.SetBitmap(hBmpDst);
mpliam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|