如何将图片保存为BMP格式?

Posted

tags:

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

你先打开电脑自带的画图程序(别说不知道),打开后,点菜单栏“文件”-“另存为”改名字的下面有个文件类型的选项,改为BMP,OK。 参考技术A 在photoshop打开您要修改的图片,另存为*.bmp格式的文件就可以了。 参考技术B 任何一个看图软件都可以啊,文件——另存为,要么用ps

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;

以上是关于如何将图片保存为BMP格式?的主要内容,如果未能解决你的问题,请参考以下文章

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

如何将摄像头采集的YUV图像数据保存成图片

一种颜色的图片我保存为gif和png格式的图片,为啥颜色会有偏差呢

如何把网页上的PHP图片下载后成为JPG格式

如何将Bitmap保存为本地图片文件?

matlab中如何修改保存图片的大小?