vc++6.0MFC画图如何保存为bmp格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vc++6.0MFC画图如何保存为bmp格式相关的知识,希望对你有一定的参考价值。

参考技术A VC6++不方便, 高于VC6版本可以用CImage, 很方便追问

但我们只学了VC6.0啊

参考技术B /****************************************************************************
*函数名称:readBmp()
*函数参数:const char *bmpName 读入bmp格式文件的名称及路径
*函数返回值:0为失败 1为成功
*函数描述:给定文件的名称和路径 读入图像的位图数据,宽,高,及每个像素的位数进内存,保存在全局变量中
*
***************************************************************************/
bool readBmp(const char* bmpName)

FILE *fp=fopen(bmpName,"rb");
if(fp==0)

printf("cannot open file");
return 0;

fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER head;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
biBitCount = head.biBitCount;
int lineByte = (bmpWidth *biBitCount/8+3)/4*4;//计算图像每行像素所占的字节数
if(biBitCount == 8)

pColorTable = new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);

pBmpBuf = new unsigned char [lineByte *bmpHeight];
fread(pBmpBuf,1,lineByte *bmpHeight,fp);
fclose(fp);
return 1;

/****************************************************************************
*函数名称: saveBmp()
*函数参数: const char *bmpName 写入bmp格式文件的名称及路径
unsigned char *imgBuf 待存盘的位图数据
int width, 以像素为单位待存盘的位图宽
int height, 以像素为单位待存盘的位图高
int biBitCount, 每个像素占的位数
RGBQUAD *pColorTable 颜色表指针
*函数返回值:0为失败 1为成功
*函数描述:给定写入bmp文件的名称和路径 要写入图像的位图数据 ,宽,高,写进文件中
*
***************************************************************************/
bool saveBmp(const char* bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable)

if(!imgBuf)//imgBuf 待存盘的位图数据
return 0;
int colorTablesize = 0;
if(biBitCount == 8)
colorTablesize =1024;
int lineByte = (width * biBitCount/8+3)/4*4;
FILE *fp = fopen(bmpName,"wb");
if(fp == 0) return 0;
BITMAPFILEHEADER fileHead;
fileHead.bfType= 0x4d42;
fileHead.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte *height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
fileHead.bfOffBits = 54 +colorTablesize;
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);
BITMAPINFOHEADER head;
head.biBitCount = biBitCount;
head.biClrImportant = 0;
head.biClrUsed = 0;
head.biCompression = 0;
head.biHeight = height;
head.biPlanes =1;
head.biSize = 40;
head.biSizeImage = lineByte *height;
head.biWidth = width;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
if(biBitCount == 8)
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
fwrite(imgBuf,height * lineByte,1,fp);
fclose(fp);
return 1;

一个简单的示例:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

unsigned char *pBmpBuf; //读入图像数据的指针
unsigned char *pNewBmpBuf;
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型,每像素位数
long LineByte; //变化后图像数据每行的字节数
bool readBmp(const char* bmpName);
bool saveBmp(const char* bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable);
void main()

char str1[80];
char str2[80];
const char* szSrcBmp;
const char* szDstBmp;
printf("输入bmp文件名称和路径");
gets(str1);
printf("输入bmp文件保存的名称和路径");
gets(str2);
szSrcBmp=str1;
szDstBmp=str2;
readBmp(szSrcBmp);
LineByte = (bmpWidth+((4-biBitCount%4)&0x03))*biBitCount/8;
pNewBmpBuf =(unsigned char*)malloc(LineByte * bmpHeight);
for(int i=0;i<bmpHeight;i++) //将原图的数据复制到另外一幅图上

for(int j=0;j<bmpWidth;j++)

for(int k=0;k<3;k++)

*(pNewBmpBuf+itmph*LineByte+itmpw*3+k) = *(pBmpBuf+itmph*LineByte+itmpw*3+k);



saveBmp(szDstBmp,pNewBmpBuf,bmpWidth,bmpHeight,biBitCount,pColorTable);
delete pNewBmpBuf;

以上是关于vc++6.0MFC画图如何保存为bmp格式的主要内容,如果未能解决你的问题,请参考以下文章

VC/MFC如何添加启动界面

我用VC++写的存图像的程序,为啥保存了的BMP图像打不开,说是照片查看器不支持此个格式,

VC++6.0MFC运行的简单流程

VC6.0把读取的图像另存为bmp图片在电脑D盘,怎么弄?

VC自绘图形怎么保存成BMP或者JPG格式?

VC怎样把文件存成BMP格式