使用BitBlt可以将屏幕复制,可是复制后还是HDC,如何将HDC转换成数组呢?或者DIB呢???
答案:1 悬赏:80
解决时间 2021-01-29 05:26
- 提问者网友:伴风望海
- 2021-01-28 15:02
使用BitBlt可以将屏幕复制,可是复制后还是HDC,如何将HDC转换成数组呢?或者DIB呢???
最佳答案
- 二级知识专家网友:话散在刀尖上
- 2021-01-28 15:11
这是我写的截屏程序hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);//获得整个屏幕的设备DC
hmemdc=CreateCompatibleDC(hdcScreen);//创建与设备DC兼容的内存DC
hbitsrc=CreateCompatibleBitmap(hdcScreen,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES)); //创建与屏幕DC兼容的位图
holdbitsrc=(HBITMAP)SelectObject(hmemdc,hbitmap);//将hbitmap指向的位图选入内存DC,返回值为原来的位图holdbitmap,先保存下来后面恢复
BitBlt(hmemdc,0,0,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES),hdcScreen,0,0,SRCCOPY);//将设备DC的图象复制到内存DC,这样hbitmap所标识的位图的LPVOIDbmBits就有图象数据了
hbitsrc=(HBITMAP)SelectObject(hmemdc,holdbitmap);//恢复内存原来的位图
pbi=CreateBitmapInfoStruct(hwnd,hbitmap);
CreateBMPFile(hwnd,"d:\\1.bmp",pbi,hbitmap,hdcScreen);
DeleteObject(hbitmap);
DeleteObject(holdbitmap);
ReleaseDC(hwnd,hdcScreen);
DeleteDC (hmemdc);
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hbitmap)
{
BITMAP bitmap;
PBITMAPINFO pbi;
WORDcClrBits;
// Retrieve the bitmap color format, width, and height.
if (!GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap))//只是获得位图信息,不包括图象数据
errhandler("GetObject", hwnd);
// Convert the color format to a count of bits.
cClrBits = (WORd)(bitmap.bmPlanes*bitmap.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)
pbi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<< cClrBits));
// There is no RGBQUAD array for the 24-bit-per-pixel format.
else
pbi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
pbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);//位图信息头的大小
pbi->bmiHeader.biWidth = bitmap.bmWidth;
pbi->bmiHeader.biHeight = bitmap.bmHeight;
pbi->bmiHeader.biPlanes = bitmap.bmPlanes;
pbi->bmiHeader.biBitCount = bitmap.bmBitsPixel;
if (cClrBits < 24)
pbi->bmiHeader.biClrUsed = (1<
// If the bitmap is not compressed, set the BI_RGB flag.
pbi->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.
pbi->bmiHeader.biSizeImage = ((pbi->bmiHeader.biWidth*cClrBits+31) & ~31)/8* pbi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbi->bmiHeader.biClrImportant = 0;
return pbi;
}
//The following example code defines a function that initializes the remaining structures, retrieves the array of palette indices, opens the file, copies the data, and closes the file.
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hbitmap, HDC hdc)
{
HANDLE fp; // file handle
BITMAPFILEHEADER bfh; // 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)
errhandler("GlobalAlloc", hwnd);
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hdc, hbitmap, 0, (WORD) pbih->biHeight,lpBits,pbi,DIB_RGB_COLORS))
{
errhandler("GetDIBits", hwnd);
}
else{
}
fp=CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (fp == INVALID_HANDLE_VALUE)
errhandler("CreateFile", hwnd);
bfh.bfType = 0x4d42;// 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
bfh.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
// Compute the offset to the array of color indices.
bfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);
if (!WriteFile(fp, (LPVOID) &bfh, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))
{
errhandler("WriteFile", hwnd);
}
if(!WriteFile(fp,(LPVOID)pbih,sizeof(BITMAPINFOHEADER)+pbih->biClrUsed*sizeof(RGBQUAD),(LPDWORD)&dwTmp,(NULL)))
errhandler("WriteFile", hwnd);
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(fp, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
errhandler("WriteFile", hwnd);
// Close the .BMP file.
if (!CloseHandle(fp))
errhandler("CloseHandle", hwnd);
// Free memory.
GlobalFree((HGLOBAL)lpBits);
}
hmemdc=CreateCompatibleDC(hdcScreen);//创建与设备DC兼容的内存DC
hbitsrc=CreateCompatibleBitmap(hdcScreen,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES)); //创建与屏幕DC兼容的位图
holdbitsrc=(HBITMAP)SelectObject(hmemdc,hbitmap);//将hbitmap指向的位图选入内存DC,返回值为原来的位图holdbitmap,先保存下来后面恢复
BitBlt(hmemdc,0,0,GetDeviceCaps(hdcScreen,HORZRES),GetDeviceCaps(hdcScreen,VERTRES),hdcScreen,0,0,SRCCOPY);//将设备DC的图象复制到内存DC,这样hbitmap所标识的位图的LPVOIDbmBits就有图象数据了
hbitsrc=(HBITMAP)SelectObject(hmemdc,holdbitmap);//恢复内存原来的位图
pbi=CreateBitmapInfoStruct(hwnd,hbitmap);
CreateBMPFile(hwnd,"d:\\1.bmp",pbi,hbitmap,hdcScreen);
DeleteObject(hbitmap);
DeleteObject(holdbitmap);
ReleaseDC(hwnd,hdcScreen);
DeleteDC (hmemdc);
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hbitmap)
{
BITMAP bitmap;
PBITMAPINFO pbi;
WORDcClrBits;
// Retrieve the bitmap color format, width, and height.
if (!GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap))//只是获得位图信息,不包括图象数据
errhandler("GetObject", hwnd);
// Convert the color format to a count of bits.
cClrBits = (WORd)(bitmap.bmPlanes*bitmap.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)
pbi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<< cClrBits));
// There is no RGBQUAD array for the 24-bit-per-pixel format.
else
pbi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
pbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);//位图信息头的大小
pbi->bmiHeader.biWidth = bitmap.bmWidth;
pbi->bmiHeader.biHeight = bitmap.bmHeight;
pbi->bmiHeader.biPlanes = bitmap.bmPlanes;
pbi->bmiHeader.biBitCount = bitmap.bmBitsPixel;
if (cClrBits < 24)
pbi->bmiHeader.biClrUsed = (1<
// If the bitmap is not compressed, set the BI_RGB flag.
pbi->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.
pbi->bmiHeader.biSizeImage = ((pbi->bmiHeader.biWidth*cClrBits+31) & ~31)/8* pbi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbi->bmiHeader.biClrImportant = 0;
return pbi;
}
//The following example code defines a function that initializes the remaining structures, retrieves the array of palette indices, opens the file, copies the data, and closes the file.
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hbitmap, HDC hdc)
{
HANDLE fp; // file handle
BITMAPFILEHEADER bfh; // 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)
errhandler("GlobalAlloc", hwnd);
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hdc, hbitmap, 0, (WORD) pbih->biHeight,lpBits,pbi,DIB_RGB_COLORS))
{
errhandler("GetDIBits", hwnd);
}
else{
}
fp=CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (fp == INVALID_HANDLE_VALUE)
errhandler("CreateFile", hwnd);
bfh.bfType = 0x4d42;// 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
bfh.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof(RGBQUAD) + pbih->biSizeImage);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
// Compute the offset to the array of color indices.
bfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed
* sizeof (RGBQUAD);
if (!WriteFile(fp, (LPVOID) &bfh, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))
{
errhandler("WriteFile", hwnd);
}
if(!WriteFile(fp,(LPVOID)pbih,sizeof(BITMAPINFOHEADER)+pbih->biClrUsed*sizeof(RGBQUAD),(LPDWORD)&dwTmp,(NULL)))
errhandler("WriteFile", hwnd);
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(fp, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
errhandler("WriteFile", hwnd);
// Close the .BMP file.
if (!CloseHandle(fp))
errhandler("CloseHandle", hwnd);
// Free memory.
GlobalFree((HGLOBAL)lpBits);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯