高分:Bmp保存为什么总是黑色的,在线等待.......

oicqkill 2005-04-06 12:55:09
Bmp保存为什么总是黑色的,在线等待.......
代码如下:
HWND hWin = GetDesktopWindow();

HDC hDc = CreateDC("DISPLAY", NULL, NULL, NULL), hDCBuffer; // GetDC(hWin);
hDCBuffer = CreateCompatibleDC( hDc );

// 创建一个与屏幕设备描述表兼容的位图
HBITMAP hmp = CreateCompatibleBitmap( hDc, 150, 40 );
SetBkColor(hDc, 24349);
SetTextColor(hDc, 34556);
SetBkMode( hDc, TRANSPARENT);
BeginPath(hDc);
TextOut( hDc, 1, 1, "fffl", 4 );
EndPath(hDc);


// BitBlt(hDCBuffer, 0, 0, 300, 300, hDc, 0, 0, SRCCOPY);
CreateBMPFile("mytest.bmp", CreateBitmapInfoStruct(hmp), hmp, hDc);

void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi,
HBITMAP hBMP, HDC hDC)
{
HANDLE hf; /* file handle */
BITMAPFILEHEADER hdr; /* bitmap file-header */
PBITMAPINFOHEADER pbih; /* bitmap info-header */
LPBYTE lpBits; /* memory pointer */
DWORD dwTotal; /* total count of bytes */
DWORD cb; /* incremental count of bytes */
BYTE *hp; /* byte pointer */
DWORD dwTmp;
pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!lpBits)
goto Exit;
/*
* Retrieve the color table (RGBQUAD array) and the bits
* (array of palette indices) from the DIB.
*/
if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS))
goto Exit;
/* Create the .BMP file. */
hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
(LPSECURITY_ATTRIBUTES) NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hf == INVALID_HANDLE_VALUE)
goto Exit;
hdr.bfType = 0x4d42; /* 0x42 = "B" 0x4d = "M" */
/* Compute the size of the entire file. */
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
/* Compute the offset to the array of color indices. */
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);
/* Copy the BITMAPFILEHEADER into the .BMP file. */
if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
goto Exit;
/* Copy the BITMAPINFOHEADER and RGBQUAD array into the file. */
if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
+ pbih->biClrUsed * sizeof (RGBQUAD),
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
goto Exit;
/* Copy the array of color indices into the .BMP file. */
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
while (cb > MAXWRITE) {
if (!WriteFile(hf, (LPSTR) hp, (int) MAXWRITE,
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
goto Exit;
cb-= MAXWRITE;
hp += MAXWRITE;
}
if (!WriteFile(hf, (LPSTR) hp, (int) cb,
(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL))
goto Exit;
/* Close the .BMP file. */
if (!CloseHandle(hf))
goto Exit;
GlobalFree((HGLOBAL)lpBits);
Exit:
return;
}


PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
/* Retrieve the bitmap's color format, width, and height. */
if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
goto Exit;
/* Convert the color format to a count of bits. */

cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else
cClrBits = 32;
/*
* Allocate memory for the BITMAPINFO structure. (This structure
* contains a BITMAPINFOHEADER structure and an array of RGBQUAD data
* structures.)
*/
if (cClrBits != 24)
pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * (2^cClrBits));
/* 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 (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = 2^cClrBits;
/* 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.
*/
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8
* pbmi->bmiHeader.biHeight
* cClrBits;
/*
* Set biClrImportant to 0, indicating that all of the
* device colors are important.
*/
pbmi->bmiHeader.biClrImportant = 0;
Exit:
return pbmi;
}
...全文
173 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Imisspassword 2005-04-14
  • 打赏
  • 举报
回复
HBITMAP hmp = CreateCompatibleBitmap( hDc, 150, 40 );
=======================================================
加上一句:
SelectObject(hDC,bmp);
=======================================================
SetBkColor(hDc, 24349);
SetTextColor(hDc, 34556);
SetBkMode( hDc, TRANSPARENT);
BeginPath(hDc);
TextOut( hDc, 1, 1, "fffl", 4 );
EndPath(hDc);

oicqkill 2005-04-11
  • 打赏
  • 举报
回复
高手们何不给我一个实在的例程呢?我的要求就时随便给个串在内存中生成图片,然后存出来
xuzheng318 2005-04-06
  • 打赏
  • 举报
回复
BITMAPINFO
This structure defines the dimensions and color information of a Windows-based device-independent bitmap (DIB).

typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;
nonocast 2005-04-06
  • 打赏
  • 举报
回复
检查一下你的BITMAPINFO
mwp 2005-04-06
  • 打赏
  • 举报
回复
太多了,我也没仔细看,我以前也遇到过同样的问题,CreateCompatibleBitmap初始化的bitmap所有位图信息都是0,保存以后就是“黑夜”,你可以用bitbilt试试,

19,468

社区成员

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

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