vc++6.0MFC画图如何保存为bmp格式 20
2018-05-19
展开全部
VC6++不方便, 高于VC6版本可以用CImage, 很方便
追问
但我们只学了VC6.0啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/****************************************************************************
*函数名称: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;
}
*函数名称: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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询