C++将数组保存成BMP图片,程序运行有误,求助怎么改

zhliy0711 2012-12-18 11:13:38
将一个数组的数据保存成一张BMP图像, 24位888格式数据, 用for循环产生了一组数,RGB(120,49,48)应该是棕红色,看到DDBdata中的数据是正确的,Bitblt语句没有把DDBdata数据赋值给DIBdata, 最后保存的图片是黑色的,程序该怎么改?
编译环境VS2088 C++ MFC

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++)
{
for( pe =0 ; pe<180*3;pe++)
{
cn = pe%3 ;
if(cn==0)
{ DDBdata[ te*540 + pe] = 48 ; }
else if (cn==1)
{ DDBdata[ te*540 + pe] = 49 ; }
else
{ DDBdata[ te*540 + pe] = 120 ; }
}
}

// SetKMode(FALSE);
CBitmap bitmap;// 图片
HBITMAP dstBmp; //DDB
bitmap.CreateBitmap(width,height,1,24,DDBdata);// 创建一张DDB位图,获得句柄
HDC hdcSrc = CreateCompatibleDC(NULL);
HDC hdcDst = CreateCompatibleDC(NULL);

BITMAPINFOHEADER bih = {0};// 位图信息头
bih.biBitCount = 24;// 每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = height;// 高度
bih.biWidth = width;// 宽度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = width*height*3;// 图像数据大小 // BI_RGB 格式时必须为

bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed=0;
bih.biClrImportant =0 ;

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

bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0 ;

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("124.bmp"),CFile::modeCreate|CFile::modeReadWrite);
file.Write(&bfh,sizeof(bfh));
file.Write(&bih,sizeof(bih));
file.Write(DIBdata,width*height*3);
file.Close();

...全文
788 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-08-26
  • 打赏
  • 举报
回复
这坟挖的……
www_adintr_com 2015-08-26
  • 打赏
  • 举报
回复
直接写文件不就行了

	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++)
	{    
		for( pe =0 ; pe<180*3;pe++)
		{
			cn = pe%3 ;
			if(cn==0)
			{ DDBdata[ te*540 + pe] = 48 ; }
			else if (cn==1)
			{ DDBdata[ te*540 + pe] = 49 ;  }
			else 
			{ DDBdata[ te*540 + pe] = 120 ;  }
		}
	}

	// SetKMode(FALSE); 
	//CBitmap bitmap;// 图片
	//HBITMAP dstBmp;   //DDB
	//bitmap.CreateBitmap(width,height,1,24,DDBdata);// 创建一张DDB位图,获得句柄
	//HDC hdcSrc = CreateCompatibleDC(NULL); 
	//HDC hdcDst = CreateCompatibleDC(NULL); 

	BITMAPINFOHEADER   bih   =   {0};// 位图信息头   
	bih.biBitCount   =   24;// 每个像素字节大小   
	bih.biCompression   =   BI_RGB;   
	bih.biHeight   =   height;// 高度   
	bih.biWidth   =   width;// 宽度  
	bih.biPlanes   =   1;   
	bih.biSize   =  sizeof(BITMAPINFOHEADER);   
	bih.biSizeImage   =  width*height*3;// 图像数据大小     // BI_RGB 格式时必须为

	bih.biXPelsPerMeter = 0;
	bih.biYPelsPerMeter = 0;   
	bih.biClrUsed=0;
	bih.biClrImportant =0 ;

	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 

	bfh.bfReserved1 = 0;
	bfh.bfReserved2 = 0 ;

	//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("124.bmp"),CFile::modeCreate|CFile::modeReadWrite); 
	file.Write(&bfh,sizeof(bfh)); 
	file.Write(&bih,sizeof(bih)); 
	file.Write(DDBdata,width*height*3); 
	file.Close();  
qq_24207887 2015-08-26
  • 打赏
  • 举报
回复
楼主你好~我也遇到和你一样的问题,请问有解决的方法吗?
WitcherLu 2012-12-26
  • 打赏
  • 举报
回复
有VC环境 正在学MFC 但是没有VS2088 帮不了你…… 好吧 其实是不会
apexMing 2012-12-26
  • 打赏
  • 举报
回复
感觉这个width和height有问题。感觉width应该是180(24位),height是240。
引用
bih.biHeight = height;// 高度 bih.biWidth = width;// 宽度
改为
引用
bih.biHeight = width ;// 高度 240 bih.biWidth = height;// 宽度 180
赵4老师 2012-12-26
  • 打赏
  • 举报
回复
提醒:bmp要求每行数据按32(也可能是别的值)字节对齐。
gotopause 2012-12-21
  • 打赏
  • 举报
回复
没VC环境,MFC没学过,帮不了你
kingdom_0 2012-12-21
  • 打赏
  • 举报
回复
有VC环境,可惜没搞过MFC,帮不了你。
图灵狗 2012-12-21
  • 打赏
  • 举报
回复
没有VC环境,帮不了你。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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