c++保存bmp文件,总是出错 5
//保存BMP文件longlTotal=0;CFilefile;file.Open(lpszPathName,CFile::modeCreate||CFile::mode...
//保存BMP文件 long lTotal=0; CFile file; file.Open(lpszPathName,CFile::modeCreate||CFile::modeReadWrite); file.Write(&bmpFH,sizeof(BITMAPFILEHEADER));///////////调试时到这一步就会出错 //lpbmi=(LPBITMAPINFO)new char[sizeof(BITMAPINFO)+4*(1<<8)]; lpbmi-="">bmiHeader.biBitCount=8; // lpbmi->bmiHeader.biClrUsed=0; // lpbmi->bmiHeader.biHeight=imageHeight; // lpbmi->bmiHeader.biWidth=imageWidth; // lpbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); // lpbmi->bmiHeader.biSizeImage=imageWidth*imageHeight; // lpbmi->bmiHeader.biClrImportant=0; // lpbmi->bmiHeader.biCompression=0; // lpbmi->bmiHeader.biPlanes=1; // lpbmi->bmiColors[i] file.Write(lpbmi,sizeof(BITMAPINFOHEADER)); //m_nColorBits=lpbmi->bmiHeader.biBitCount; //imageHeight=lpbmi->bmiHeader.biHeight; //imageWidth=lpbmi->bmiHeader.biWidth; if(ColorBits==8) { lTotal=imageWidth*imageHeight; file.Write(&(lpbmi->bmiColors[0]),256*4); }else if(ColorBits==24) { lTotal=imageWidth*imageHeight*3; }else { file.Close(); return FALSE; } //m_pBits=new unsigned char[lTotal]; file.Write(m_pBits,lTotal); file.Close(); return TRUE;<!--
展开
1个回答
展开全部
file.Open(lpszPathName,CFile::modeCreate||CFile::modeReadWrite);逻辑或改成file.Open(lpszPathName,CFile::modeCreate|CFile::modeReadWrite);按位或
#include <afx.h>
BOOL SaveBmpFile(LPCTSTR lpszPathName, long imageHeight, long imageWidth, int ColorBits, void * m_pBits)
{
long lTotal = 0;
int i;
BOOL ret = TRUE;
CFile file;
if ( !file.Open( lpszPathName, CFile::modeCreate | CFile::modeReadWrite) )
{
_tprintf( TEXT("Can't open file %s\n"), lpszPathName);
return FALSE;
}
BITMAPFILEHEADER bmpFH;
bmpFH.bfType = 0x4d42; // 'MB'
bmpFH.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(tagRGBQUAD) * (ColorBits <= 8 ? (1 << ColorBits) : 0);
bmpFH.bfSize = bmpFH.bfOffBits + (((imageWidth * ColorBits + 31) & ~31) >> 3) * imageHeight;
bmpFH.bfReserved1 = 0;
bmpFH.bfReserved2 = 0;
LPBITMAPINFO lpbmi = (LPBITMAPINFO)new BYTE[sizeof(BITMAPINFO) + 4 * (1 << 8)];
lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biWidth = imageWidth;
lpbmi->bmiHeader.biHeight = imageHeight;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = ColorBits;
lpbmi->bmiHeader.biCompression = BI_RGB;
lpbmi->bmiHeader.biSizeImage = (((imageWidth * ColorBits + 31) & ~31) >> 3) * imageHeight;
lpbmi->bmiHeader.biXPelsPerMeter = 3000;
lpbmi->bmiHeader.biYPelsPerMeter = 3000;
lpbmi->bmiHeader.biClrUsed = 0;
lpbmi->bmiHeader.biClrImportant = 0;
for(i = 0; i < 256; i++)
{
lpbmi->bmiColors[i].rgbBlue = i;
lpbmi->bmiColors[i].rgbGreen = i;
lpbmi->bmiColors[i].rgbRed = i;
lpbmi->bmiColors[i].rgbReserved = 0;
}
switch(ColorBits)
{
case 1:
case 4:
case 8:
file.Write(&bmpFH, sizeof(BITMAPFILEHEADER));
file.Write(lpbmi, sizeof(BITMAPINFOHEADER));
file.Write(lpbmi->bmiColors, sizeof(tagRGBQUAD) * (1 << ColorBits)); // 调色板
file.Write(m_pBits, lpbmi->bmiHeader.biSizeImage);
break;
case 16:
case 24:
case 32:
file.Write(&bmpFH, sizeof(BITMAPFILEHEADER));
file.Write(lpbmi, sizeof(BITMAPINFOHEADER));
file.Write(m_pBits, lpbmi->bmiHeader.biSizeImage);
break;
default:
ret = FALSE;
}
file.Close();
delete [] (BYTE *)lpbmi;
return ret;
}
int main()
{
char buf[60000];
int i;
for(i=0; i<60000; i++) buf[i] = (char) rand();
SaveBmpFile("3.bmp", 200, 300, 8, buf);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询