⑴ VC中如何读取图片文件并将数据赋值给一个CString类型的变量
打开文件,获取图像数据,把数据存放在内存中,注意最后一个字节一定是0,最后把该地址指针赋值给CString类型的变量即可。
⑵ C语言打开图像文件后读取像素
C语言打开图像文件后运用以下代码就可以读取像素,具体如下:#ifndef IMAGE_H#define IMAGE_Hvoid image_info(FILE* file);void image_save(FILE *file);void image_gray();void image_binarization();void image_opposite();void image_channel(); //抽取RGB通道void image_bright();//改变图像亮度typedef struct BMP{ //14字节 unsigned short bfType; //文件标识 2字节 必须为BM unsigned int bfSize; //文件大小 4字节 unsigned short bfReserved1; //保留,每字节以"00"填写 2字节 unsigned short bfReserved2; //同上 2字节 unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节 //40字节 unsigned int biSize; //表示本结构的大小 4字节 int biWidth; //位图的宽度 4字节 int biHeight; //位图的高度 4字节 unsigned short biPlanes; //永远为1 , 2字节 unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节 unsigned int biCompression; //压缩说明 4字节 unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节 int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节 int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节 unsigned int biClrUsed; //位图使用的颜色索引数 4字节 unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节 } BMP;int line_byte;unsigned char *imagedata;extern BMP bmp;extern int line_byte;extern unsigned char *imagedata;#endif//image_rw.c文件#include<stdio.h>#include<stdlib.h>#include"image.h"void image_info(FILE *file){int times=3; //输入文件名次数。 char bmp_name[10]; //文件名 printf("\nplease enter a file name for reading:"); do { if (times<3) { printf("\nplease enter a file name for reading again:"); } fflush(stdin); gets(bmp_name); //printf("\n%s",bmp_name); file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。 –times; if (file==NULL) { printf("\nerror opening %s for reading! ",bmp_name); } else { break; } } while(times!=0); if (times==0) { printf("\nsorry, shutdown!"); exit(1); }//读取图像信息 fseek(file,0L,0); //读取图像文件类型 fread(&bmp,sizeof(BMP),1,file); printf("\n bmp tpye: %u",bmp.bfType); printf("\n bmp size: %u",bmp.bfSize); printf("\n bmp reserved1: %u",bmp.bfReserved1); printf("\n bmp reserved2: %u",bmp.bfReserved2); printf("\n bmp offBits: %u",bmp.bfOffBits); printf("\n bmp bisize: %u",bmp.biSize); printf("\n bmp biWidth: %d",bmp.biWidth); printf("\n bmp biHeight: %d",bmp.biHeight); printf("\n bmp biplans: %u",bmp.biPlanes); printf("\n bmp biBitCount: %u",bmp.biBitCount); printf("\n bmp biCompression: %u",bmp.biCompression); printf("\n bmp biSizeImage: %u",bmp.biSizeImage); printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter); printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter); printf("\n bmp biClrUsed: %u",bmp.biClrUsed); printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数 //printf("dfsa%u",bmp.line_byte); //bmp.imagedata=NULL; imagedata=(unsigned char*)malloc(bmp.biSizeImage); fseek(file,(long)bmp.bfOffBits,0); fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file); fclose(file);}//保存图像void image_save(FILE *file){ int times=3; //输入文件名次数。 char bmp_name[10]; //文件名 //int i; //记录数据区个数 printf("\nplease enter a file name for writeing:"); do { if (times<3) { printf("\nplease enter a file name for writeing again:"); } fflush(stdin); gets(bmp_name); printf("\n%s",bmp_name); file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。 –times; if (file==NULL) { printf("\nerror opening %s for writing",bmp_name); } else { break; } } while(times!=0); if (times==0) { printf("\nsorry, shutdown!"); exit(1); }//写文件头 printf("\n%s",bmp_name); fseek(file,0L,0); //图像文件类型 fwrite(&(bmp.bfType),sizeof(short),1,file); printf("\n bmp tpye: %d",bmp.bfType); fseek(file,2L,0); //图像文件大小 fwrite(&(bmp.bfSize),sizeof(int),1,file); printf("\n bmp size: %d",bmp.bfSize); fseek(file,6L,0); //图像文件保留字1 fwrite(&(bmp.bfReserved1),sizeof(short),1,file); printf("\n bmp reserved1: %d",bmp.bfReserved1); fseek(file,8L,0); //图像文件保留字2 fwrite(&(bmp.bfReserved2),sizeof(short),1,file); printf("\n bmp reserved2: %d",bmp.bfReserved2); fseek(file,10L,0);//数据区的偏移量 fwrite(&(bmp.bfOffBits),sizeof(short),1,file); printf("\n bmp offBits: %d",bmp.bfOffBits);fseek(file,14L,0);//文件头结构大小 fwrite(&(bmp.biSize),sizeof(int),1,file); printf("\n bmp bisize: %d",bmp.biSize);fseek(file,18L,0);//图像的宽度 fwrite(&(bmp.biWidth),sizeof(int),1,file); printf("\n bmp biWidth: %d",bmp.biWidth); fseek(file,22L,0);//图像的高度 fwrite(&(bmp.biHeight),sizeof(int),1,file); printf("\n bmp biHeight: %d",bmp.biHeight);fseek(file,24L,0);//图像的面数 fwrite(&(bmp.biPlanes),sizeof(short),1,file); printf("\n bmp biplans: %d",bmp.biPlanes); fseek(file,28L,0);//图像一个像素的字节数 fwrite(&(bmp.biBitCount),sizeof(short),1,file); printf("\n bmp biBitCount: %d",bmp.biBitCount); fseek(file,30L,0);//图像压缩信息 fwrite(&(bmp.biCompression),sizeof(short),1,file); printf("\n bmp biCompression: %d",bmp.biCompression); fseek(file,34L,0);//图像数据区的大小 fwrite(&(bmp.biSizeImage),sizeof(int),1,file); printf("\n bmp biSizeImage: %d",bmp.biSizeImage); fseek(file,38L,0);//水平分辨率 fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file); printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter); fseek(file,42L,0);//垂直分辨率 fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file); printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter); fseek(file,46L,0);//颜色索引数 fwrite(&(bmp.biClrUsed),sizeof(int),1,file); printf("\n bmp biClrUsed: %d",bmp.biClrUsed); fseek(file,50L,0);//重要颜色索引数 fwrite(&(bmp.biClrImportant),sizeof(int),1,file); printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);fseek(file,(long)(bmp.bfOffBits),0); fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file); fclose(file);}//pixProcess.c文件#include<stdio.h>#include<stdlib.h>#include<math.h>#include"image.h"//灰度化void image_gray(){ int i,j; unsigned char tmp; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2)); imagedata[i*line_byte+j*3+0]=tmp; imagedata[i*line_byte+j*3+1]=tmp; imagedata[i*line_byte+j*3+2]=tmp; //printf("\nnidsfh%d %d",i,j); } }}//二值化 void image_binarization(){ int i,j; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { if ((*(imagedata+i*line_byte+j))<128) { imagedata[i*line_byte+j]=0; } else { imagedata[i*line_byte+j]=255; } } }}void image_opposite() //反相{ int i,j; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]); } }}void image_channel() //抽取RGB通道{ int i,j; char rgb; printf("\nplease enter a char(r/g/b): "); fflush(stdin); scanf("%c",&rgb); if (rgb=='b') { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j+1]=0; imagedata[i*line_byte+3*j+2]=0; } } } else if(rgb=='g') { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j]=0; imagedata[i*line_byte+3*j+2]=0; } } } else { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j]=0; imagedata[i*line_byte+3*j+1]=0; } } }}void image_bright()//改变图像亮度{ int level; int i,j; printf("\n please enter the level of brightness[-255 to 255] :"); fflush(stdin); scanf("%d",&level); for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { if (level>=0) { if ((imagedata[i*line_byte+j]+level)>255) imagedata[i*line_byte+j]=255; else imagedata[i*line_byte+j]+=level; } else { if ((imagedata[i*line_byte+j]-abs(level))<0) imagedata[i*line_byte+j]=0; else imagedata[i*line_byte+j]+=level; } } }}//void image_create() //创建一幅24位BMP图像文件。//{//main.c文件#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>#include"image.h"BMP bmp;int main(){ FILE *file=NULL; int choose; char gono; do { image_info(file); //imagedata已经分配了动态内存,但是没有释放 printf("\n 1.image_opposite"); printf("\n 2.image_gray"); printf("\n 3.image_binarization"); printf("\n 4.image_channel"); printf("\n 5.image_brightness"); //printf("6.image_opposite"); //printf("7.image_opposite"); printf("\nchoose your options:"); fflush(stdin); scanf("%d",&choose); switch(choose) { case 1: image_opposite(); image_save(file); free(imagedata); break; case 2: image_gray(); image_save(file); free(imagedata); break; case 3: image_binarization(); image_save(file); free(imagedata); break; case 4: image_channel(); image_save(file); free(imagedata); break; case 5: image_bright(); image_save(file); free(imagedata); break; default: printf("\n wrong choose!"); } printf("\nlet's go on?(y/n):"); fflush(stdin); scanf("%c",&gono); if (gono=='n') { printf("\nbye bye!"); break; } } while(1); return 0;}
⑶ C语言中如何读取图片
你需要读取图片干什么?
opencv库就是专门用来处理图片的
opencv
⑷ 请问如何使用纯C语言读取文件中的图片,并将图片存储在二维数组中
1、使用双层循环语句,就可以依次把数据顺序读入到一个二维数组当中了。2、例程:#include#include#defineMAXLINE3#defineMAXCOLUMN10voidmain(void){FILE*fp;//文件指针chararr[MAXLINE][MAXCOLUMN]={0};//定义3行10列的二维数组并初始化inti=-1;if((fp=fopen("./test/filename.txt","r"))==NULL){//打开txt文件perror("Fileopenerror!\n");return;}while((fgets(arr[++i],MAXCOLUMN+1,fp))!=NULL)//读取一行并存到arr数组printf("%d:",i);//打印行号//puts(arr[i]);char*subarr=strtok(arr[i],"");//以空格为分隔符从arr[i]中获得字串while(subarr!=NULL){data[i][j]=atoi(subarr);//将字串转为int型数据存入data数组printf("%d\t",data[i][j]);//打印data[i][jsubarr=strtok(NULL,"");//继续获得arr[i]中的字串j++;//data数组列加一}printf("\n");}//循环完毕后,所有数据已在data数组中printf("\n");fclose(fp);//关闭指针}
⑸ 怎样用c语言读入一个图片文件,为了进行二值化等一些列的处理
#include <stdio.h> #include <stdlib.h>#include <conio.h>#pragma pack(1)#define R 30#define G 59#define B 11#define ONE 255 #define ZERO 0 typedef unsigned short WORD;typedef unsigned long DWORD;typedef long LONG;typedef unsigned char BYTE;typedef struct tagBITMAPFILEHEADER { // bmfh WORD bfType; // 位图文件的类型,必须为BM DWORD bfSize; // 位图文件的大小,以字节为单位 WORD bfReserved1; // 位图文件保留字,必须为0 WORD bfReserved2; // 位图文件保留字,必须为0 DWORD bfOffBits; // 位图数据的起始位置,以相对于位图文件头的偏移量表示,以字节为单位} BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER{ // bmih DWORD biSize; // 本结构所占用字节数 LONG biWidth; // 位图的宽度,以像素为单位 LONG biHeight; // 位图的高度,以像素为单位 WORD biPlanes; // 目标设备的级别,必须为1 WORD biBitCount;// 每个像素所需的位数,必须是1(双色),4(16色),8(256色)或24(真彩色)之一 DWORD biCompression; // 位图压缩类型,必须是 0(不压缩),1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一 DWORD biSizeImage; // 位图的大小,以字节为单位 LONG biXPelsPerMeter; // 位图水平分辨率,每米像素数 LONG biYPelsPerMeter; // 位图垂直分辨率,每米像素数 DWORD biClrUsed;// 位图实际使用的颜色表中的颜色数 DWORD biClrImportant;// 位图显示过程中重要的颜色数} BITMAPINFOHEADER; typedef struct tagPOINT{ BYTE b; BYTE g; BYTE r;} POINT;int quit();int quit(){ puts("File's format wrong"); exit(0);}void main (int argc,char *argv[]) { FILE *fi,*fo;//I/O file char fin[80],fon[80];//I/O file name BYTE buff,o=0; BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; POINT **p; int i,j,t; if(argc<3) { printf("orginfile name:"); scanf("%s",fin); printf("resultfile name:"); scanf("%s",fon); }else{ sscanf(argv[1],"%s",fin); sscanf(argv[2],"%s",fon); } if(argc==4) sscanf(argv[4],"%d",&t); else{ printf("theshold [0,255]:"); scanf("%d",&t); } if (((fi=fopen(fin,"rb"))==NULL)||((fo=fopen(fon,"wb"))==NULL)) { puts("\nfile open failed"); return; } fread(&bf,sizeof(bf),1,fi); fread(&bi,sizeof(bi),1,fi); if(bf.bfType!=0x4d42) quit(); if(bf.bfReserved1!=0x0000) quit(); if(bf.bfReserved2!=0x0000) quit(); if(bi.biClrImportant!=0) quit(); if(bi.biBitCount!=0x18) quit(); if(bi.biCompression!=0) quit(); if(bi.biPlanes!=1) quit(); bf.bfSize=54+1024+bi.biWidth*bi.biHeight; bf.bfOffBits=54+1024; if(bi.biWidth%4==0) bi.biSizeImage=bi.biWidth*bi.biHeight*bi.biBitCount/8; else bi.biSizeImage=(bi.biWidth-(bi.biWidth%4)+4)*bi.biHeight*bi.biBitCount/8; bi.biBitCount=8; p=(POINT **)malloc(sizeof(POINT *)*bi.biHeight); for (i=0;i<bi.biHeight;i++) *(p+i)=(POINT *)malloc(sizeof(POINT)*bi.biWidth); //分配失败后果自负! fwrite(&bf,sizeof(bf),1,fo); fwrite(&bi,sizeof(bi),1,fo); for (i=0;i<bi.biHeight;i++) for (j=0;j<bi.biWidth;j++) fread(*(p+i)+j,sizeof(POINT),1,fi); for (i=0x00,buff=0x00;i<=0xFF;i++,buff++) { fwrite(&buff,sizeof(buff),1,fo); fwrite(&buff,sizeof(buff),1,fo); fwrite(&buff,sizeof(buff),1,fo); fwrite(&o,sizeof(o),1,fo); } for (i=0;i<bi.biHeight;i++) for (j=0;j<bi.biWidth;j++) { buff=((*(p+i)+j)->r*R+(*(p+i)+j)->g*G+(*(p+i)+j)->b*B)/100; buff=(buff>=t?ONE:ZERO) fwrite(&buff,sizeof(buff),1,fo); } fclose(fo); }
⑹ c语言怎样打开JPG文件
一般来讲是生成ppmorpnm处理图片数据的,但是如果你非要读取JPG格式的数据,我想下面的Library可以帮助到你.http://www.ijg.org/
⑺ 用c语言如何读取和保存jpg图片文件
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==",pFile);
printf("%d",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
(7)c读取图片文件扩展阅读:
c语言读取TXT文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
int main()
{
char buf[MAX_LINE]; /*缓冲区*/
FILE *fp; /*文件指针*/
int len; /*行字符个数*/
if((fp = fopen("test.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
while(fgets(buf,MAX_LINE,fp) != NULL)
{
len = strlen(buf);
buf[len-1] = ''; /*去掉换行符*/
printf("%s %d ",buf,len – 1);
}
return 0;
}
⑻ 怎么用C语言中的fopen函数打开bmp格式的图像文件
1.图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。2.例程:
#include<stdlib.h>#include<stdio.h>intmain(){FILE*fpPhoto,*fpText,*fpTarget;intiRead;charszBuf[100];printf("请输入第一个文件名(jpg):");gets(szBuf);fpPhoto=fopen(szBuf,"rb");printf("请输入第二个文件名(txt):");gets(szBuf);fpText=fopen(szBuf,"rb");printf("请输入目的文件名(jpg):");gets(szBuf);fpTarget=fopen(szBuf,"wb");if(!fpPhoto||!fpText||!fpTarget){printf("打开文件失败!");system("pause");return-1;}while((iRead=fread(szBuf,1,sizeof(szBuf),fpPhoto))>0)fwrite(szBuf,1,iRead,fpTarget);while((iRead=fread(szBuf,1,sizeof(szBuf),fpText))>0)fwrite(szBuf,1,iRead,fpTarget);fclose(fpPhoto);fclose(fpText);fclose(fpTarget);return0;}
⑼ c语言,怎样读取一个BMP图片
#ifndef IMAGE_H#define IMAGE_Hvoid image_info(FILE* file);void image_save(FILE *file);void image_gray();void image_binarization();void image_opposite();void image_channel(); //抽取RGB通道void image_bright();//改变图像亮度typedef struct BMP{ //14字节 unsigned short bfType; //文件标识 2字节 必须为BM unsigned int bfSize; //文件大小 4字节 unsigned short bfReserved1; //保留,每字节以"00"填写 2字节 unsigned short bfReserved2; //同上 2字节 unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节 //40字节 unsigned int biSize; //表示本结构的大小 4字节 int biWidth; //位图的宽度 4字节 int biHeight; //位图的高度 4字节 unsigned short biPlanes; //永远为1 , 2字节 unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节 unsigned int biCompression; //压缩说明 4字节 unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节 int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节 int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节 unsigned int biClrUsed; //位图使用的颜色索引数 4字节 unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节 } BMP;int line_byte;unsigned char *imagedata;extern BMP bmp;extern int line_byte;extern unsigned char *imagedata;#endif//image_rw.c文件#include<stdio.h>#include<stdlib.h>#include"image.h"void image_info(FILE *file){int times=3; //输入文件名次数。 char bmp_name[10]; //文件名 printf("\nplease enter a file name for reading:"); do { if (times<3) { printf("\nplease enter a file name for reading again:"); } fflush(stdin); gets(bmp_name); //printf("\n%s",bmp_name); file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。 –times; if (file==NULL) { printf("\nerror opening %s for reading! ",bmp_name); } else { break; } } while(times!=0); if (times==0) { printf("\nsorry, shutdown!"); exit(1); }//读取图像信息 fseek(file,0L,0); //读取图像文件类型 fread(&bmp,sizeof(BMP),1,file); printf("\n bmp tpye: %u",bmp.bfType); printf("\n bmp size: %u",bmp.bfSize); printf("\n bmp reserved1: %u",bmp.bfReserved1); printf("\n bmp reserved2: %u",bmp.bfReserved2); printf("\n bmp offBits: %u",bmp.bfOffBits); printf("\n bmp bisize: %u",bmp.biSize); printf("\n bmp biWidth: %d",bmp.biWidth); printf("\n bmp biHeight: %d",bmp.biHeight); printf("\n bmp biplans: %u",bmp.biPlanes); printf("\n bmp biBitCount: %u",bmp.biBitCount); printf("\n bmp biCompression: %u",bmp.biCompression); printf("\n bmp biSizeImage: %u",bmp.biSizeImage); printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter); printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter); printf("\n bmp biClrUsed: %u",bmp.biClrUsed); printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数 //printf("dfsa%u",bmp.line_byte); //bmp.imagedata=NULL; imagedata=(unsigned char*)malloc(bmp.biSizeImage); fseek(file,(long)bmp.bfOffBits,0); fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file); fclose(file);}//保存图像void image_save(FILE *file){ int times=3; //输入文件名次数。 char bmp_name[10]; //文件名 //int i; //记录数据区个数 printf("\nplease enter a file name for writeing:"); do { if (times<3) { printf("\nplease enter a file name for writeing again:"); } fflush(stdin); gets(bmp_name); printf("\n%s",bmp_name); file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。 –times; if (file==NULL) { printf("\nerror opening %s for writing",bmp_name); } else { break; } } while(times!=0); if (times==0) { printf("\nsorry, shutdown!"); exit(1); }//写文件头 printf("\n%s",bmp_name); fseek(file,0L,0); //图像文件类型 fwrite(&(bmp.bfType),sizeof(short),1,file); printf("\n bmp tpye: %d",bmp.bfType); fseek(file,2L,0); //图像文件大小 fwrite(&(bmp.bfSize),sizeof(int),1,file); printf("\n bmp size: %d",bmp.bfSize); fseek(file,6L,0); //图像文件保留字1 fwrite(&(bmp.bfReserved1),sizeof(short),1,file); printf("\n bmp reserved1: %d",bmp.bfReserved1); fseek(file,8L,0); //图像文件保留字2 fwrite(&(bmp.bfReserved2),sizeof(short),1,file); printf("\n bmp reserved2: %d",bmp.bfReserved2); fseek(file,10L,0);//数据区的偏移量 fwrite(&(bmp.bfOffBits),sizeof(short),1,file); printf("\n bmp offBits: %d",bmp.bfOffBits);fseek(file,14L,0);//文件头结构大小 fwrite(&(bmp.biSize),sizeof(int),1,file); printf("\n bmp bisize: %d",bmp.biSize);fseek(file,18L,0);//图像的宽度 fwrite(&(bmp.biWidth),sizeof(int),1,file); printf("\n bmp biWidth: %d",bmp.biWidth); fseek(file,22L,0);//图像的高度 fwrite(&(bmp.biHeight),sizeof(int),1,file); printf("\n bmp biHeight: %d",bmp.biHeight);fseek(file,24L,0);//图像的面数 fwrite(&(bmp.biPlanes),sizeof(short),1,file); printf("\n bmp biplans: %d",bmp.biPlanes); fseek(file,28L,0);//图像一个像素的字节数 fwrite(&(bmp.biBitCount),sizeof(short),1,file); printf("\n bmp biBitCount: %d",bmp.biBitCount); fseek(file,30L,0);//图像压缩信息 fwrite(&(bmp.biCompression),sizeof(short),1,file); printf("\n bmp biCompression: %d",bmp.biCompression); fseek(file,34L,0);//图像数据区的大小 fwrite(&(bmp.biSizeImage),sizeof(int),1,file); printf("\n bmp biSizeImage: %d",bmp.biSizeImage); fseek(file,38L,0);//水平分辨率 fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file); printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter); fseek(file,42L,0);//垂直分辨率 fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file); printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter); fseek(file,46L,0);//颜色索引数 fwrite(&(bmp.biClrUsed),sizeof(int),1,file); printf("\n bmp biClrUsed: %d",bmp.biClrUsed); fseek(file,50L,0);//重要颜色索引数 fwrite(&(bmp.biClrImportant),sizeof(int),1,file); printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);fseek(file,(long)(bmp.bfOffBits),0); fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file); fclose(file);}//pixProcess.c文件#include<stdio.h>#include<stdlib.h>#include<math.h>#include"image.h"//灰度化void image_gray(){ int i,j; unsigned char tmp; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2)); imagedata[i*line_byte+j*3+0]=tmp; imagedata[i*line_byte+j*3+1]=tmp; imagedata[i*line_byte+j*3+2]=tmp; //printf("\nnidsfh%d %d",i,j); } }}//二值化 void image_binarization(){ int i,j; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { if ((*(imagedata+i*line_byte+j))<128) { imagedata[i*line_byte+j]=0; } else { imagedata[i*line_byte+j]=255; } } }}void image_opposite() //反相{ int i,j; for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]); } }}void image_channel() //抽取RGB通道{ int i,j; char rgb; printf("\nplease enter a char(r/g/b): "); fflush(stdin); scanf("%c",&rgb); if (rgb=='b') { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j+1]=0; imagedata[i*line_byte+3*j+2]=0; } } } else if(rgb=='g') { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j]=0; imagedata[i*line_byte+3*j+2]=0; } } } else { for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte/3;j++) { imagedata[i*line_byte+3*j]=0; imagedata[i*line_byte+3*j+1]=0; } } }}void image_bright()//改变图像亮度{ int level; int i,j; printf("\n please enter the level of brightness[-255 to 255] :"); fflush(stdin); scanf("%d",&level); for (i=0;i<bmp.biHeight;i++) { for (j=0;j<line_byte;j++) { if (level>=0) { if ((imagedata[i*line_byte+j]+level)>255) imagedata[i*line_byte+j]=255; else imagedata[i*line_byte+j]+=level; } else { if ((imagedata[i*line_byte+j]-abs(level))<0) imagedata[i*line_byte+j]=0; else imagedata[i*line_byte+j]+=level; } } }}//void image_create() //创建一幅24位BMP图像文件。//{//main.c文件#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>#include"image.h"BMP bmp;int main(){ FILE *file=NULL; int choose; char gono; do { image_info(file); //imagedata已经分配了动态内存,但是没有释放 printf("\n 1.image_opposite"); printf("\n 2.image_gray"); printf("\n 3.image_binarization"); printf("\n 4.image_channel"); printf("\n 5.image_brightness"); //printf("6.image_opposite"); //printf("7.image_opposite"); printf("\nchoose your options:"); fflush(stdin); scanf("%d",&choose); switch(choose) { case 1: image_opposite(); image_save(file); free(imagedata); break; case 2: image_gray(); image_save(file); free(imagedata); break; case 3: image_binarization(); image_save(file); free(imagedata); break; case 4: image_channel(); image_save(file); free(imagedata); break; case 5: image_bright(); image_save(file); free(imagedata); break; default: printf("\n wrong choose!"); } printf("\nlet's go on?(y/n):"); fflush(stdin); scanf("%c",&gono); if (gono=='n') { printf("\nbye bye!"); break; } } while(1); return 0;}
⑽ 如何用c语言读取图片
#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *&img, int w, int h)
{img = new unsigned char[w*h];}
void delateimage(unsigned char*img)
{delete []img;}
void readimage(unsigned char*img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp,fname, "rb");
if (fp == NULL){ cout << "error" << endl; return; }
size_t result;
result=fread(img , sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Reading error" << endl;
return;
}
else
cout << "Reading Ok!" << endl;
fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])
{
for (int i = 0; i for (int j = 0; j if (iw – 3 || j>h – 3)
image1[Twoto1(i,j,w)] = 0;
else
{
float temp = 0;
for (int m = 0; m<5; m++)
for (int n = 0; n<5; n++)
{
temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);
}
if (temp>255) image1[Twoto1(i, j, w)] = 255;
else if (temp<0) image1[Twoto1(i, j, w)] = 0;
else image1[Twoto1(i, j, w)] = temp;
}
}
void saveimage(unsigned char *img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp, fname, "wb");
if (fp == NULL) { cout << "error" << endl; return; }
size_t result;
result = fwrite(img, sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Write error" << endl;
return;
}
else
cout << "Write Ok!" << endl;
fclose(fp);
}
void main()
{
unsigned char *img;
unsigned char *img1;
float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
//float moban[5][5] = { 0 };
int w = 512, h = 512;
createimage(img, w, h);
createimage(img1, w, h);
readimage(img, w, h, "E:ss.raw");
mobanjuanji(img, img1,w, h, moban);
saveimage(img, w, h, "E:ss_1.raw");
saveimage(img1, w, h, "E:ss_2.raw");
delateimage(img);
delateimage(img1);
}
(10)c读取图片文件扩展阅读
C语言实现一个图片的读出和写入
#include <stdlib.h>
#include <windows.h>
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==",pFile);
printf("%d",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
未经允许不得转载:山九号 » c读取图片文件|怎么用C语言中的fopen函数打开bmp格式的图像文件