#include "Windows.h"
#include <stdio.h>
unsigned char *pBmpBuf;
unsigned char *pBmpBuf1;
int bmpWidth;
int bmpHeight;
RGBQUAD *pColorTable;
int biBitCount;
bool readBmp(char *bmpName)
{
FILE *fp=fopen(bmpName,"rb");
if(fp==0) 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;
}
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height,
int biBitCount, RGBQUAD *pColorTable)
{
if(!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;
}
void main()
{
char readPath[]="1.BMP";
readBmp(readPath);
printf("width=%d,height=%d, biBitCount=%d\n",bmpWidth,bmpHeight, biBitCount);
char writePath[]="2.BMP";
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);
delete []pBmpBuf;
if(biBitCount==8)
delete []pColorTable;
}
VC6.0图像读取与保存
需积分: 12 129 浏览量
2018-05-12
19:58:45
上传
评论
收藏 2.4MB ZIP 举报
qq_19411455
- 粉丝: 2
- 资源: 6
最新资源
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市 源码下载
- VMware7.0虚拟机硬盘无法编辑,无法连接到Profile-Driven Storage Service
- arm64内核的mongo镜像
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
- nhit完整源码+论文学习
- 足球比赛结果统计表2006-2011年大约28W场比赛
- 基于PHP+mysql的社区交流系统(源代码)
- yolov5,SSD 可能使用到的一些代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈