镜像位图,这里提供两个函数供大家复制使用。
效果执行后的位图如下
MFC如何镜像位图
函数一:DrawInvertedBitmap
DrawInvertedBitmap()在指定位置绘制位图的镜像图,
它使用 StretchBlt()进行镜像。
当源和目标的宽度的相反时,图像进行横向镜像;反之,进行纵向镜像。
// 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,
int xDest, int yDest )
{
// Create a memory DC compatible with the destination DC
CDC memDC;
memDC.CreateCompatibleDC( pDC );
// Get logical coordinates
BITMAP bm;
::GetObject( hBitmap, sizeof( bm ), &bm );
//memDC.SelectObject( &bitmap );
HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );
// Select and realize the palette
if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
pDC->RealizePalette();
}
if( bLateral )
pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC,
bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
else
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 );
}
函数二:GetInvertedBitmap
GetInvertedBitmap()函数创建新的位图来存储镜像位图,
该函数使用 StretchBlt()进行镜像。
在这种情况下,目标设备环境是另外一个内存设备环境,这样,图像就被显示在位图上。
// 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
CDC sourceDC, destDC;
sourceDC.CreateCompatibleDC( NULL );
destDC.CreateCompatibleDC( NULL );
// Get logical coordinates
BITMAP bm;
::GetObject( hBitmap, sizeof( bm ), &bm );
// Create a bitmap to hold the result
HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL),
bm.bmWidth, bm.bmHeight);
// Select bitmaps into the DCs
HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );
if( bLateral )
destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC,
bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
else
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 );
return hbmResult;
}
|