当前位置: 代码迷 >> Windows Mobile >> 在MOBILE下,CBitmap:GetBitmapBits不能用,有其他方法吗?解决方法
  详细解决方案

在MOBILE下,CBitmap:GetBitmapBits不能用,有其他方法吗?解决方法

热度:203   发布时间:2016-04-25 08:01:25.0
在MOBILE下,CBitmap::GetBitmapBits不能用,有其他方法吗?
我在ppc上做抓屏时,得到屏幕位图的句柄m_hBmpScreen,
然后转换成CBitmap   *PBmp=CBitmap::FromHandle(m_hBmpScreen);
可要取位图数据时发现PBmp-> GetBitmapBits()函数不能用,在mobile下CBitmap没这个成员函数,
请教各位大虾,有何高招?

------解决方案--------------------
显示图片需要在onPaint()l里实现
The following code fragment sums up how to paint a bitmap:

// Create a DC that matches the device.
hdcMem = CreateCompatibleDC (hdc);

// Select the bitmap into the compatible device context.
hOldSel = SelectObject (hdcMem, hBitmap);

// Get the bitmap dimensions from the bitmap.
GetObject (hBitmap, sizeof (BITMAP), &bmp);

// Copy the bitmap image from the memory DC to the screen DC.
BitBlt (hdc, rect.left, rect.top, bmp.bmWidth, bmp.bmHeight,
hdcMem, 0, 0, SRCCOPY);

// Restore original bitmap selection and destroy the memory DC.
SelectObject (hdcMem, hOldSel);
DeleteDC (hdcMem);

------解决方案--------------------
void GetDibBits(HBITMAP hSourceBitmap, DWORD& dwSize,
LPVOID& pBuffer, DWORD& dwWidth, DWORD& dwHeight)
{
if (!hSourceBitmap)
return;

//1. Getting bimap size.
BITMAP bm;
GetObject(hSourceBitmap, sizeof(BITMAP), &bm);


//2. Creating new bitmap and receive pointer to it 's bits.
HBITMAP hTargetBitmap;

//2.1 Initilize DIBINFO structure
DIBINFO dibInfo;
dibInfo.bmiHeader.biBitCount = 24;
dibInfo.bmiHeader.biClrImportant = 0;
dibInfo.bmiHeader.biClrUsed = 0;
dibInfo.bmiHeader.biCompression = 0;
dibInfo.bmiHeader.biHeight = bm.bmHeight;
dibInfo.bmiHeader.biPlanes = 1;
dibInfo.bmiHeader.biSize = 40;
dibInfo.bmiHeader.biSizeImage = bm.bmWidth*bm.bmHeight*3;
dibInfo.bmiHeader.biWidth = bm.bmWidth;
dibInfo.bmiHeader.biXPelsPerMeter = 3780;
dibInfo.bmiHeader.biYPelsPerMeter = 3780;
dibInfo.bmiColors[0].rgbBlue = 0;
dibInfo.bmiColors[0].rgbGreen = 0;
dibInfo.bmiColors[0].rgbRed = 0;
dibInfo.bmiColors[0].rgbReserved = 0;

dwSize = dibInfo.bmiHeader.biSizeImage;
dwWidth = bm.bmWidth;
dwHeight = bm.bmHeight;

//2.2 Create bitmap and receive pointer to points into pBuffer
HDC hDC = ::GetDC(NULL);
ASSERT(hDC);
hTargetBitmap = CreateDIBSection( hDC,
(const BITMAPINFO*)dibInfo,
DIB_RGB_COLORS,
(void**)&pBuffer,
NULL,
0);

::ReleaseDC(NULL, hDC);


//3. Copy source bitmap into the target bitmap.

//3.1 Create 2 device contexts
CDC memDc;
if (!memDc.CreateCompatibleDC(NULL))
{
ASSERT(FALSE);
}

CDC targetDc;
if (!targetDc.CreateCompatibleDC(NULL))
{
ASSERT(FALSE);
}

//3.2 Select source bitmap into one DC, target into another
HBITMAP hOldBitmap1 = (HBITMAP)::SelectObject(memDc.GetSafeHdc(), hSourceBitmap);
HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject(targetDc.GetSafeHdc(), hTargetBitmap);

//3.3 Copy source bitmap into the target one
targetDc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDc, 0, 0, SRCCOPY);

//3.4 Restore device contexts
::SelectObject(memDc.GetSafeHdc(), hOldBitmap1);
::SelectObject(targetDc.GetSafeHdc(), hOldBitmap2);
memDc.DeleteDC();
targetDc.DeleteDC();

DeleteObject(hSourceBitmap);
DeleteObject(hTargetBitmap);
  相关解决方案