3×2的bitmap为什么无法预览?而8x1的却可以

WindTech 2006-12-10 01:05:19

无法预览,在windows画板中可以打开

代码是这样的:
//bitmap.cx = 3; bitmap.cy = 2
//strFileName = 3-2.bmp

long lBufferSize = bitmap.cx * bitmap.cy * 3;

HANDLE hf = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, NULL, NULL );

if( hf == INVALID_HANDLE_VALUE )
return;

// write out the file header
//
BITMAPFILEHEADER bfh;
memset( &bfh, 0, sizeof( bfh ) );
bfh.bfType = 'MB';
bfh.bfSize = sizeof( bfh ) + lBufferSize + sizeof( BITMAPINFOHEADER );
bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );

DWORD dwWritten = 0;
WriteFile( hf, &bfh, sizeof( bfh ), &dwWritten, NULL );

// and the bitmap format
//
BITMAPINFOHEADER bih;
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = bitmap.cx;
bih.biHeight = bitmap.cy;
bih.biPlanes = 1;
bih.biBitCount = 24;

dwWritten = 0;
WriteFile( hf, &bih, sizeof( bih ), &dwWritten, NULL );

// and the bits themselves
//
dwWritten = 0;
WriteFile( hf, pBuffer, lBufferSize, &dwWritten, NULL );
CloseHandle( hf );
...全文
120 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
WindTech 2006-12-10
  • 打赏
  • 举报
回复
非常感谢,能再问下为什么 扫描线要4字节对齐?不熟悉
lambochan 2006-12-10
  • 打赏
  • 举报
回复
扫描线要4字节对齐,8x1当然正常,而3x2的:
long lBufferSize = bitmap.cx * bitmap.cy * 3;
是不能这么乘的.
=============================================
long WidthBytes = bitmap.cx * 3;
if(WidthBytes % 4 != 0)
WidthBytes = (WidthBytes / 4 + 1) * 4;
long lBufferSize = WidthBytes * bitmap.cy;
=================================================

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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