我们可以垂直或水平转换图像。
函数1: DrawInvertedBitmap
DrawInvertedBitmap()函数绘制一个反转的图像到指定的位置。它使用
StretchBlt()函数实现这些。如果源宽度与目标宽度不同,那么沿X轴反转,
如果源高度与目标高度不同,那么沿Y轴反转。
// DrawInvertedBitmap
Draws the bitmap after inverting it
// hBimtap
- Bitmap handle
// hPal
Palette to use when drawing the bitmap
// bLateral
- Flag to indicate whether to invert laterally or vertically
// xDest
- X coordinate of top left corner to draw at
// yDest
- Y coordinate of top left corner to draw at
void DrawInvertedBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, BOOL bLateral,
nt xDest, int yDest )
/ Create a memory DC compatible with the destination DC
DC memDC;
emDC.CreateCompatibleDC( pDC );
/ Get logical coordinates
ITMAP bm;
:GetObject( hBitmap, sizeof( bm ), &bm );
/memDC.SelectObject( &bitmap );
BITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );
/ Select and realize the palette
f( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
pDC->RealizePalette();
f( bLateral )
pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC,
bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
lse
pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC,
0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );
/ Restore the old bitmap
:SelectObject( memDC.m_hDC, hBmOld );
函数 2: GetInvertedBitmap
GetInvertedBitmap()函数创建新的位图用来保存反转后的位图。
// GetInvertedBitmap
Creates a new bitmap with the inverted image
// Returns
- Handle to a new bitmap with inverted image
// hBitmap
- Bitmap to invert
// bLateral
- Flag to indicate whether to invert laterally or vertically
HBITMAP GetInvertedBitmap( HBITMAP hBitmap, BOOL bLateral )
/ Create a memory DC compatible with the display
DC sourceDC, destDC;
ourceDC.CreateCompatibleDC( NULL );
estDC.CreateCompatibleDC( NULL );
/ Get logical coordinates
ITMAP bm;
:GetObject( hBitmap, sizeof( bm ), &bm );
/ Create a bitmap to hold the result
BITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL),
bm.bmWidth, bm.bmHeight);
/ Select bitmaps into the DCs
BITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
BITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );
f( bLateral )
destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC,
bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
lse
destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC,
0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );
/ Reselect the old bitmaps
:SelectObject( sourceDC.m_hDC, hbmOldSource );
:SelectObject( destDC.m_hDC, hbmOldDest );
eturn hbmResult;