16,473
社区成员
发帖
与我相关
我的任务
分享
BYTE* DDBdata = new BYTE[240*180*3];
BYTE* DIBdata;
WORD width=240;
WORD height=180;
int te,pe,cn;
for(te=0; te<240; te++)
{
cn=0;
for( pe =0 ; pe<180*3;pe++)
{
if(cn==0)
{ DDBdata[ te*540 + pe] = 50 ;cn++;}
else if (cn==1)
{ DDBdata[ te*540 + pe] = 80 ;cn++;}
else
{ DDBdata[ te*540 + pe] = 120 ; cn=0;}
}
}
CBitmap bitmap;// 图片
HBITMAP dstBmp;
bitmap.CreateBitmap(width,height,1,24,DDBdata);// 创建一张位图
HDC hdcSrc = CreateCompatibleDC(NULL);
HDC hdcDst = CreateCompatibleDC(NULL);
BITMAPINFOHEADER bih = {0};// 位图信息头
bih.biBitCount = 24;// 每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = height;// 高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = 0;// width*height*2;// 图像数据大小 // BI_RGB 格式时 必须为0
bih.biWidth = width;// 宽度
BITMAPFILEHEADER bfh = {0};// 位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);// 到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + width*height*3; // 文件总的大小
bfh.bfType = (WORD)0x4d42; // must be "MB" 0x4d = 77 = M , 0x42 = 66 = B
BITMAPINFO bi={0};
bi.bmiHeader=bih;
dstBmp=CreateDIBSection(hdcDst, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void **)&DIBdata, NULL, 0);
SelectObject(hdcDst, dstBmp);
SelectObject(hdcSrc, bitmap);
BitBlt(hdcDst, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);// 将位图复制到实际的设备环境中
CFile file(_T("123.bmp"),CFile::modeCreate|CFile::modeReadWrite);
file.Write(&bfh,sizeof(bfh));
file.Write(&bih,sizeof(bih));
file.Write(DIBdata,width*height*3);
file.Close();
//delete []DDBdata;