資源簡介
c語言BMP文件加水印,可使用devc++打開,使用c語言給BMP文件加水印

代碼片段和文件信息
#include?
#include?
#include?
unsigned?char?*pBmpBuf;//讀入圖像數(shù)據(jù)的指針
int?bmpWidth;//圖像的寬
int?bmpHeight;//圖像的高
RGBQUAD?*pColorTable;//顏色表指針
int?biBitCount;//圖像類型,每像素位數(shù)
int?readBmp(char?*bmpName){
FILE?*fp=fopen(bmpName“rb“);//二進(jìn)制讀方式打開指定的圖像文件
if(fp==0){
printf(“打水印失敗\n“);
return?0;
}
fseek(fp?sizeof(BITMAPFILEHEADER)0);//跳過位圖文件頭結(jié)構(gòu)BITMAPFILEHEADER
BITMAPINFOHEADER?head;??//定義位圖信息頭結(jié)構(gòu)變量,讀取位圖信息頭進(jìn)內(nèi)存,存放在變量head中
fread(&head?sizeof(BITMAPINFOHEADER)?1fp);?
bmpWidth?=?head.biWidth;
bmpHeight?=?head.biHeight;
biBitCount?=?head.biBitCount;//獲取圖像寬、高、每像素所占位數(shù)等信息
int?lineByte=(bmpWidth?*?biBitCount/8+3)/4*4;//定義變量,計(jì)算圖像每行像素所占的字節(jié)數(shù)(必須是4的倍數(shù))
if(biBitCount==8){//灰度圖像有顏色表,且顏色表表項(xiàng)為256
pColorTable=malloc(sizeof(RGBQUAD)*256);?//申請顏色表所需要的空間,讀顏色表進(jìn)內(nèi)存
fread(pColorTablesizeof(RGBQUAD)256fp);
}
pBmpBuf=malloc(sizeof(unsigned?char)*lineByte?*?bmpHeight);//申請位圖數(shù)據(jù)所需要的空間,讀位圖數(shù)據(jù)進(jìn)內(nèi)存
fread(pBmpBuf1lineByte?*?bmpHeightfp);
fclose(fp);//關(guān)閉文件
return?1;
}
int?saveBmp(char?*bmpName?unsigned?char?*imgBuf?int?width?int?height?int?biBitCount?RGBQUAD?*pColorTable){
if(!imgBuf)//如果位圖數(shù)據(jù)指針為0,則沒有數(shù)據(jù)傳入,函數(shù)返回
return?0;
int?colorTablesize=0;//顏色表大小,以字節(jié)為單位,灰度圖像顏色表為1024字節(jié),彩色圖像顏色表大小為0
if(biBitCount==8)
colorTablesize=1024;
int?lineByte=(width?*?biBitCount/8+3)/4*4;//待存儲圖像數(shù)據(jù)每行字節(jié)數(shù)為4的倍數(shù)
FILE?*fp=fopen(bmpName“wb“);//以二進(jìn)制寫的方式打開文件
if(fp==0){
printf(“!!!!\n“);
return?0;
}
BITMAPFILEHEADER?fileHead;//申請位圖文件頭結(jié)構(gòu)變量,填寫文件頭信息
fileHead.bfType?=?0x4D42;//bmp類型
fileHead.bfSize=?sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colorTablesize+lineByte*height;//bfSize是圖像文件4個(gè)組成部分之和
fileHead.bfReserved1?=?0;
fileHead.bfReserved2?=?0;
fileHead.bfOffBits=54+colorTablesize;//bfOffBits是圖像文件前3個(gè)部分所需空間之和
fwrite(&fileHead?sizeof(BITMAPFILEHEADER)1?fp);//寫文件頭進(jìn)文件
BITMAPINFOHEADER?head;?//申請位圖信息頭結(jié)構(gòu)變量,填寫信息頭信息
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);//寫位圖信息頭進(jìn)內(nèi)存
?
if(biBitCount==8)//如果灰度圖像,有顏色表,寫入文件?
fwrite(pColorTable?sizeof(RGBQUAD)256?fp);
fwrite(imgBuf?height*lineByte?1?fp);//寫位圖數(shù)據(jù)進(jìn)文件
fclose(fp);//關(guān)閉文件
?return?1;
}
int?main(){
int?o=0;//RGB計(jì)數(shù)器?
printf(“BMP文件錄入位置:\nE:/project/devcpp/img/input1.BMP\nE:/project/devcpp/img/input2.BMP\n\n“);
char?readPath1[]=“E:/project/devcpp/img/input1.BMP“;//讀入指定BMP文件進(jìn)內(nèi)存
readBmp(readPath1);
unsigned?char?*pBmpBuf1=pBmpBuf;;//讀入圖像數(shù)據(jù)的指針
int?bmpWidth1=bmpWidth;//圖像的寬
int?bmpHeight1=bmpHeight;//圖像的高
RGBQUAD?*pColorTable1=pColorTable;//顏色表指針
int?biBitCount1=biBitCount;//圖像類型,每像素位數(shù)
char?readPath2[]=“E:/project/devcpp/img/input2.BMP“;//讀入指定BMP文件進(jìn)內(nèi)存
readBmp(readPath2);
unsigned?char?*pBmpBuf2=pBmpBuf;;//讀入圖像數(shù)據(jù)的指針
int?bmpWi
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-06-07?18:12??BMP?WaterMark\
?????文件????????5120??2017-09-08?10:01??BMP?WaterMark\BMPWaterMark.c
?????文件?????????918??2017-08-30?08:49??BMP?WaterMark\BMPWaterMark.dev
?????文件?????1116885??2017-09-08?10:01??BMP?WaterMark\BMPWaterMark.exe
?????文件??????????94??2017-09-08?11:03??BMP?WaterMark\BMPWaterMark.layout
?????文件??????986887??2017-09-08?10:01??BMP?WaterMark\BMPWaterMark.o
?????文件????????1180??2017-09-08?10:01??BMP?WaterMark\Makefile.win
評論
共有 條評論