中易网

使用BitBlt可以将屏幕复制,可是复制后还是HDC,如何将HDC转换成数组呢?或者DIB呢???

答案:1  悬赏:80  
解决时间 2021-01-29 05:26
使用BitBlt可以将屏幕复制,可是复制后还是HDC,如何将HDC转换成数组呢?或者DIB呢???
最佳答案
这是我写的截屏程序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);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
3.3x9.9怎么算
长期坐在电脑前有什么危害
1个月内多少位京官外放?
外射有那么容易怀孕吗
《武侠乂》在出生地看到别人有一招(空手空中
买房子时说的板楼和塔楼各是什么意思?有什么
Professor Lee"s book will show you__can
唐山新华联铂尔曼大酒店地址在什么地方,想过
氨根离子里面的氮元素为什么显负三价?不是应
清·钮琇《觚賸·石言》:“此中石时有蔚蓝者
山东沂蒙山海拔
15岁女生c罩杯好吗?
以张纾恺三个字写一首诗
竖式计算0.119除以17
青雅网吧地址在什么地方,我要处理点事
推荐资讯
我长期承包别村的土地遇到了县城建设!县里给
女友的问题给我难住了 希望聪明人给我解答
324除以79怎么验算和用竖式计算
天津福乐康是传销吗
中国近代关于友谊的材料
草苹种类
标志408换前轮轴承后abs,esp故障灯点亮哪的事
玩他妈什么欲擒故众还深冬声东击西。 这句话
《命中注定我爱你》续集有吗?
荣耀7i和vivo x6哪个更好?比如自拍,卡顿,
美国签证更新EVUS系统有时间限制吗
北汽绅宝D50这款车怎么样啊
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?