用程序读入BMP的信息,将该信息重新写出生成图片时,图片右侧一小部分移到了图片的左侧。请各位帮我看看问题出在哪,帮帮我,谢谢!
代码如下:
```c++
typedef struct {
BITMAP bmp; // BITMAP構造体
RGBQUAD *rgb ;
} BMPDATA;
bool ReadBmp(string strbmpName, int & m_nBiBitCount)
{
FILE *fp = new FILE;
int resOpen = fopen_s(&fp, strbmpName.c_str(), "rb");
if (resOpen)
{
return false;
}
fseek(fp, sizeof(BITMAPFILEHEADER), 0);
//BITMAPFILEHEADER filehead;
//fread(&filehead, 1, sizeof(BITMAPFILEHEADER), fp);
BMPDATA* m_pBitmapData = new BMPDATA;
BITMAPINFOHEADER bitmapInfoHeader;
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, fp); //获取图像宽、高、每像素所占位数等信息
m_nBiBitCount = bitmapInfoHeader.biBitCount;//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
m_pBitmapData->bmp.bmType = 0;
m_pBitmapData->bmp.bmWidth = bitmapInfoHeader.biWidth;
m_pBitmapData->bmp.bmHeight = bitmapInfoHeader.biHeight;
int lineByte = (m_pBitmapData->bmp.bmWidth * m_nBiBitCount / 8 + 3) / 4 * 4;//灰度图像有颜色表,且颜色表表项为256
//int lineByte = ((((m_pBitmapData->bmp.bmWidth * m_nBiBitCount) + 31) & ~31) >> 3);
m_pBitmapData->bmp.bmWidthBytes = lineByte;
m_pBitmapData->bmp.bmPlanes = bitmapInfoHeader.biPlanes; //颜色平面数
m_pBitmapData->bmp.bmBitsPixel = bitmapInfoHeader.biBitCount;
if (m_nBiBitCount == 8)
{
//申请颜色表所需要的空间,读颜色表进内存
RGBQUAD *pQuad = new RGBQUAD[256];
fread(pQuad, sizeof(RGBQUAD), COLOR_TABLE_ENTRIES, fp);
m_pBitmapData->rgb = pQuad;
}
m_pBitmapData->bmp.bmBits = new unsigned char[lineByte * m_pBitmapData->bmp.bmHeight];
fread(m_pBitmapData->bmp.bmBits, sizeof(UCHAR), lineByte * m_pBitmapData->bmp.bmHeight, fp);
fclose(fp);//关闭文件
return true;
}
bool SaveBmp(string strBmpName, BMPDATA *m_pBitmapData, int m_nBiBitCount)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if (m_pBitmapData == NULL)
{
return false;
}
if (!m_pBitmapData->bmp.bmBits)
{
return false;
}
//颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
int colorTablesize = 0;
if (m_nBiBitCount == 8)
{
colorTablesize = 1024;
}
//以二进制写的方式打开文件
FILE *fp = new FILE;
int resOpen = fopen_s(&fp, strBmpName.c_str(), "wb");
if (resOpen)
{
return false;
}
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize +
m_pBitmapData->bmp.bmWidthBytes * m_pBitmapData->bmp.bmHeight;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bfOffBits = 54 + colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER infohead;
infohead.biBitCount = m_nBiBitCount;
infohead.biClrImportant = 0;
infohead.biClrUsed = 0;
infohead.biCompression = 0;
infohead.biHeight = m_pBitmapData->bmp.bmHeight;
infohead.biPlanes = 1;
infohead.biSize = 40;
infohead.biSizeImage = m_pBitmapData->bmp.bmWidthBytes * sizeof(RGBTRIPLE) * abs(m_pBitmapData->bmp.bmHeight);
infohead.biWidth = m_pBitmapData->bmp.bmWidth;
infohead.biXPelsPerMeter = 0;
infohead.biYPelsPerMeter = 0;
//写位图信息头进内存
fwrite(&infohead, sizeof(BITMAPINFOHEADER), 1, fp);
//如果灰度图像,有颜色表,写入文件
if (m_nBiBitCount == 8 && m_pBitmapData->rgb)
{
fwrite(m_pBitmapData->rgb, sizeof(RGBQUAD), COLOR_TABLE_ENTRIES, fp);
}
//写位图数据进文件
fwrite(m_pBitmapData->bmp.bmBits, m_pBitmapData->bmp.bmHeight * m_pBitmapData->bmp.bmWidthBytes, 1, fp);
//关闭文件
fclose(fp);
return true;
}
```