11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10. Bitmap<br />

Sample 10.1-2\GDI<br />

Sample 10.1-2\GDI demonstrates how to use function CDC::StretchBlt(…) to output the bitmap<br />

image. It is based on sample 10.1-1\GDI. In this sample, the image is enlarged to twice of its original size<br />

and output to the client window.<br />

Because the target image has a bigger dimension now, we need to adjust the scroll sizes. First function<br />

CGDIView::OnInitialUpdate() is modified as follows for this purpose:<br />

……<br />

void CGDIView::OnInitialUpdate()<br />

{<br />

SetScrollSizes(MM_TEXT, CSize(2*bm.bmWidth, 2*bm.bmHeight));<br />

}<br />

CScrollView::OnInitialUpdate();<br />

The scroll sizes are set to twice of the bitmap size.<br />

Function CDC::StretchBlt(…) has 10 parameters:<br />

BOOL CDC::StretchBlt<br />

(<br />

int x, int y, int nWidth, int nHeight,<br />

CDC *pSrcDC,<br />

int xSrc, int ySrc, int nSrcWidth, int nSrcHeight,<br />

DWORD dwRop<br />

);<br />

There are two extra parameters nSrcWidth and nSrcHeight here (compared to function<br />

CDC::BitBlt()), which specify the extent of original bitmap that will be output to the target. Obviously,<br />

nWidth and nSrcWidth determine the horizontal ratio of the output bitmap (relative to source bitmap).<br />

Likewise, nHeight and nSrcHeight determine the vertical ratio.<br />

In the sample, both horizontal and vertical ratios are set to 200%, and function CGDIView::OnDraw(…)<br />

is modified as follows:<br />

……<br />

void CGDIView::OnDraw(CDC *pDC)<br />

{<br />

}<br />

pDC->StretchBlt<br />

(<br />

0,<br />

0,<br />

2*bm.bmWidth,<br />

2*bm.bmHeight,<br />

&dcMemory,<br />

0,<br />

0,<br />

bm.bmWidth,<br />

bm.bmHeight,<br />

SRCCOPY<br />

);<br />

dcMemory.SelectObject(pBmpOld);<br />

With the above modifications, we will have an enlarged bitmap image in the client window.<br />

10.2 Extracting Palette from DIB<br />

If we execute the previous two samples on a palette device, we may experience color distortion (Please<br />

open file 10.1-1\bitmap.bmp using a standard graphic editor and compare the results). This is because we<br />

didn’t implement logic palette for displaying the bitmap, so the the colors of the bitmap pixels are mapped<br />

285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!