从内存DC创建bmp文件

http://zhidao.baidu.com/question/27257644.html

HDC hScrDC, hMemDC;         
int width, height;


//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL; 


//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);


//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);


//creates a memory device context (DC) compatible with the screen device(hScrDC) 
hMemDC = CreateCompatibleDC(hScrDC);


//initialise the struct BITMAPINFO for the bimap infomation, 
//in order to use the function CreateDIBSection
// on wince os, each pixel stored by 24 bits(biBitCount=24) 
//and no compressing(biCompression=0) 
BITMAPINFO RGB24BitsBITMAPINFO; 
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;


//use the function CreateDIBSection and SelectObject 
//in order to get the bimap pointer : lpBitmapBits 
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO, 
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);


// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);


//if you only want to get the every pixel color value, 
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;


//bimap file header in order to write bmp file 
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp 
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8)


//write into file
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))

//write bitmap file header    //文件头
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info           //数据头
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data    //数据信息
fwrite(lpBitmapBits, 3*width*height, 1, mStream);
//close file
fclose(mStream);

#ifdef _UNICODE
WCHAR wBuffer[1024] ;
mbstowcs(wBuffer, filename, strlen(filename)+1);
CStringW str;
str.Format(_T("文件保存在: "));
str.AppendFormat(wBuffer);
MessageBoxW(str);
TRACE(str);
#else
CStringA str;
str.format("文件保存在: ");
str.AppendFormat(filename);
MessageBoxA(str);


#endif //_UNICODE

}


//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老猿的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值