缓冲区中的位图如何现实

cyberkit 2006-06-13 03:35:15
从网络接收到一个位图,如何显示到窗体上
我用CreateBitmap,然后DrawState显示的位图上下反转了,为什么啊?
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycxm5502 2006-06-14
  • 打赏
  • 举报
回复
哟,你的文件在网络上么, 没做过帮顶!!!
cyberkit 2006-06-14
  • 打赏
  • 举报
回复
不但反转了,而且运行多次后就出错!!!为什么
cyberkit 2006-06-14
  • 打赏
  • 举报
回复
代码如下:
你可能会人为有些地方多此一举
为什么得到的位图上下反转了???搞了两天啦!

void CShowScreenDlg::OnOK()
{
// 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, (WORD) pbih->biHeight, lpBits, pbmi,
DIB_RGB_COLORS))
{
AfxMessageBox("GetDIBits");
}

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);
GlobalFree((HGLOBAL)lpBits);
}
cyberkit 2006-06-13
  • 打赏
  • 举报
回复
那应该怎么反过来
livedeal 2006-06-13
  • 打赏
  • 举报
回复
数据反了~~~~BMP吗~???看一下它的数据是怎么存的

19,469

社区成员

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

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