bmp位图存储问题

dushisemang 2016-03-28 05:42:06
截图写出数据

我用c++ 写出位图 大小为1*1 像素 显示正常 24真色彩 输出

但是 我用 16进制查看文件的时候 数据不对呀

应该是54字节后+ 4字节的数据 (3字节颜色和1个字节的对齐)

16进制查看有 7字节 ???

有时候是正确的 4字节



...全文
281 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dushisemang 2016-03-28
  • 打赏
  • 举报
回复
截取图像的关键代码



//截取图片
bool pixelex::captrue(long x,long y,long w,long h,char* filepath)
{
	//路径处理
//	setpath(filepath);
	filepath="c:/123.txt";
	long pixelLength;
	byte * pixelDate=NULL;
	FILE * wfile=NULL;
	//打开文件
	wfile = fopen( filepath, "wb" );
	fwrite( head, 54, 1, wfile );
	//更改grab.bmp的头文件中的高度和宽度
	fseek( wfile, 0x0012, SEEK_SET );
	fwrite( &w, sizeof(w), 1, wfile );
	fwrite( &h, sizeof(h), 1, wfile );
	//为像素分配内存
	pixelLength = w * 3;
	if ( pixelLength % 4 != 0 )
	{
		pixelLength += 4 - pixelLength%4;
	}
	pixelLength *= h;
	pixelDate = (byte *)malloc( pixelLength );
	if ( pixelDate == 0 )
	{
		AfxMessageBox(L"内存分配失败");
	}
	
	if(ishaverc)
	{
		glReadPixels(x, y, w, h, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixelDate );
		glFlush();
//		AfxMessageBox(L"有");
	}else
	{
		bool ok=apihook::getdx()->readpix(x,y,w,h,pixelDate);
		if(!ok)
		{
			fclose( wfile );
			free( pixelDate );
			return false;
		}	
//		AfxMessageBox(L"没有");
	}


	CString o;
	o.Format(L"%d",pixelLength);
	AfxMessageBox(o);
       //这里输出一直是4没有错

	fseek( wfile, 0, SEEK_END );
	fwrite( pixelDate, pixelLength, 1, wfile );
	fclose( wfile );
	free( pixelDate );
	return true;
}







dushisemang 2016-03-28
  • 打赏
  • 举报
回复
关键一点 不是bmp 应该是4字节对齐的 但是7个字节 显示确是正常的黄色。。。。。。

1,222

社区成员

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

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