导航:首页 > 软件知识 > c语言程序怎么看图片

c语言程序怎么看图片

发布时间:2023-05-25 09:51:56

① 如何用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);

}

(1)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语言中如何显示图片

1、图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。
2、例程:

#include<stdlib.h>
#include<stdio.h>
intmain()
{
FILE*fpPhoto,*fpText,*fpTarget;
intiRead;
charszBuf[100];
printf洞猜("请输圆拿入第一个文件名(bmp): ");
gets(szBuf);
fpPhoto=fopen(szBuf,"rb");
printf("请输入第二个文件名(txt): ");
gets(szBuf);
fpText=fopen(szBuf,"rb");
printf("请输入目的文件名(bmp): ");
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语言程序怎么看图片相关的资料

热点内容
电缆技术参数怎么填写 浏览:274
商品交易失信怎么办 浏览:286
微信发错信息怎么恢复 浏览:222
统计学和数据专业哪个好 浏览:717
社会技术学什么比较好 浏览:850
新旧iphone怎么数据同步 浏览:753
大盘交易量10点前说明什么 浏览:717
a股t0交易什么时候正式实施 浏览:672
淮北二手房交易多少套 浏览:989
王者荣耀数据和账户哪个可删 浏览:124
南宁有哪些二手货车交易市场 浏览:812
投资人的信息怎么找 浏览:642
邯郸复兴综合农贸市场怎么样 浏览:678
如何在手机上代理社保签到 浏览:123
登机牌选号哪个小程序 浏览:989
如何做小米产品销售员 浏览:367
持卡人交易确认函是什么 浏览:474
一级代理需要什么资料 浏览:531
黄金交易后怎么盈利 浏览:614
静态代理商有哪些 浏览:688