为什么显示的图象上下反转了?

cyberkit 2006-07-10 04:54:31
代码如下:

// TODO: Add extra validation here
HBITMAP hOldBitmap, hbmScreen;

HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
HDC hdcCompatible = CreateCompatibleDC(hdcScreen);

// Create a compatible bitmap for hdcScreen.

hbmScreen = CreateCompatibleBitmap(hdcScreen,
GetDeviceCaps(hdcScreen, HORZRES),
GetDeviceCaps(hdcScreen, VERTRES));

if (hbmScreen == 0)
{
AfxMessageBox("hbmScreen");
return;
}

// Select the bitmaps into the compatible DC.
hOldBitmap =(HBITMAP)SelectObject(hdcCompatible, hbmScreen);

if (!hOldBitmap)
{
AfxMessageBox("Compatible Bitmap Selection");
return;
}

//Copy color data for the entire display into a
//bitmap that is selected into a compatible DC.

if (!BitBlt(hdcCompatible,
0,0,
GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES),
hdcScreen,
0,0,
SRCCOPY))
{
AfxMessageBox("Screen to Compat Blt Failed");
return;
}

hbmScreen = (HBITMAP) SelectObject(hdcCompatible, hOldBitmap);
//==================================================================

BITMAP bmp;
PBITMAPINFO pbmi;
WORD BitsPerPixel;

// Retrieve the bitmap color format, width, and height.
//根据位图句柄获取位图信息
if (!GetObject(hbmScreen, sizeof(BITMAP), (LPSTR)&bmp))
{
AfxMessageBox("GetObject");
return;
}

// Convert the color format to a count of bits.
BitsPerPixel = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (BitsPerPixel == 1)
BitsPerPixel = 1;
else if (BitsPerPixel <= 4)
BitsPerPixel = 4;
else if (BitsPerPixel <= 8)
BitsPerPixel = 8;
else if (BitsPerPixel <= 16)
BitsPerPixel = 16;
else if (BitsPerPixel <= 24)
BitsPerPixel = 24;
else BitsPerPixel = 32;

// Allocate memory for the BITMAPINFO structure. (This structure
// contains a BITMAPINFOHEADER structure and an array of RGBQUAD
// data structures.)

if (BitsPerPixel != 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * (1<< BitsPerPixel));

// There is no RGBQUAD array for the 24-bit-per-pixel format.

else
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER));

// Initialize the fields in the BITMAPINFO structure.

pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (BitsPerPixel < 24)
pbmi->bmiHeader.biClrUsed = (1<<BitsPerPixel);

// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;

// Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
// For Windows NT, the width must be DWORD aligned unless
// the bitmap is RLE compressed. This example shows this.
// For Windows 95/98/Me, the width must be WORD aligned unless the
// bitmap is RLE compressed.
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * BitsPerPixel +31) & ~31) /8 * pbmi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0;

//===================================================================
PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER) pbmi;

LPBYTE lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!GetDIBits(hdcCompatible, hbmScreen, 0, pbih->biHeight, lpBits, pbmi,
DIB_RGB_COLORS))
{
AfxMessageBox("GetDIBits");
return;
}

DWORD ErrCode = ::GetLastError();
if (ErrCode != 0)
{
CString ErrString;
ErrString.Format("%d", ErrCode);
AfxMessageBox(ErrString);
}

HBITMAP hBitmap = ::CreateBitmap(pbih->biWidth, pbih->biHeight, pbih->biPlanes, pbih->biBitCount, lpBits);
CDC *pDC = GetDC();
pDC->DrawState(CPoint(0,0), CSize(400,300), hBitmap, DST_BITMAP);

ReleaseDC(pDC);
DeleteDC(hdcScreen);
DeleteDC(hdcCompatible);

::DeleteObject(hOldBitmap);
::DeleteObject(hbmScreen);
::DeleteObject(hBitmap);
GlobalFree((HGLOBAL)lpBits);
...全文
352 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
navyblue1982 2006-07-11
  • 打赏
  • 举报
回复
pbmi->bmiHeader.biHeight = bmp.bmHeight;
改为pbmi->bmiHeader.biHeight = -bmp.bmHeight; 就可以了
cyberkit 2006-07-11
  • 打赏
  • 举报
回复
TO EddieCai()

那怎样才能反转过来啊
EddieCai 2006-07-11
  • 打赏
  • 举报
回复
很简单啊,你在文件里从前往后读,像素从下往上画~
EddieCai 2006-07-10
  • 打赏
  • 举报
回复
不想多看了,就想问一下,你是在bitmap里一个点一个点读出来显示的吗?

如果是的话,问题可能在于,其实位图里面的信息是从下往上存的,就是说最后一行的信息存在最前面,第一行存在最后面。

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧