完整程序_C语言对BMP图像的读和写和对像素的操作
#include
#include "Windows.h"
BOOL readBmp(char *bmpName);
BOOL saveBmp(char *bmpName, char *imgBuf, int width, int heigh, int biBitCount, RGBQUAD *pColorTable);
char *pBmpBuf; //位图数据
int bmpWidth; // 图像宽度
int bmpHeight; //图像高度
int biBiCount; //图像类型,每像素位数
RGBQUAD *pColorTable; //位图颜色表指针
int main()
{
char readName[] = "read.BMP";
readBmp(readName);
char writeName[] = "write.BMP";
saveBmp(writeName, pBmpBuf, bmpWidth, bmpHeight, biBiCount, pColorTable); int lineByte = (bmpWidth*bmpHeight/8+3)/4*4;
if (biBiCount == 8)
{
for (int i = 0; i < bmpWidth/2; i++)
{
for (int j = 0; j < bmpHeight/2; j++)
{
*(pBmpBuf+i*lineByte+j) = 0;
}
}
}
else if (biBiCount == 24)
{/对于24位真彩图,每个像素占三个字节分别存储R、G、B三个颜色分量的颜色值 for (int i = 0; i < bmpWidth/2; i++)
{
for (int j = 0; j < bmpHeight/2; j++)
{
for (int k = 0; k < 3; k++)
*(pBmpBuf+i*lineByte+j*3+k) = 0; //将rgb三个颜色分量设置成黑色
}
}
}
char Name[] = "copy.BMP";