用c++语言打开/保存一幅图片的代码?
c++初学者,要求用c++语言打开一幅图片,将其二值化后保存。求打开和保存的代码,小弟分不多,望高人赐教,谢过,在线等……...
c++初学者,要求用c++语言打开一幅图片,将其二值化后保存。求打开和保存的代码,小弟分不多,望高人赐教,谢过,在线等……
展开
2013-06-06
展开全部
#include<iostream>using namespace std; void OpenAndSavePic()
{
char name[50];//文件名
int length;//文件长度
char *buf;//文件内容
memset(name, 0, 50);
cout << "输入要打开的图片:";
cin >> name;
FILE * f = fopen(name, "rb");//二进制方式 if(!f)
{
cout << "图片" << name << "打开失败" << endl;
return;
} fseek(f, 0, SEEK_END);
length = ftell(f);
buf = new char[length + 1];
fseek(f, 0, SEEK_SET); fread(buf, length, 1, f);
buf[length] = '\0'; fclose(f); memset(name, 0, 50);
cout << "输入要保存的文件名:";
cin >> name;
f = fopen(name, "wb");
if(!f)
{
cout << "保存图片" << name << "失败" << endl;
return;
}
cout << "文件" << name << "保存完毕" << endl; fwrite(buf, length, 1, f);
fclose(f);
}int main()
{
OpenAndSavePic();
return 0;
}
{
char name[50];//文件名
int length;//文件长度
char *buf;//文件内容
memset(name, 0, 50);
cout << "输入要打开的图片:";
cin >> name;
FILE * f = fopen(name, "rb");//二进制方式 if(!f)
{
cout << "图片" << name << "打开失败" << endl;
return;
} fseek(f, 0, SEEK_END);
length = ftell(f);
buf = new char[length + 1];
fseek(f, 0, SEEK_SET); fread(buf, length, 1, f);
buf[length] = '\0'; fclose(f); memset(name, 0, 50);
cout << "输入要保存的文件名:";
cin >> name;
f = fopen(name, "wb");
if(!f)
{
cout << "保存图片" << name << "失败" << endl;
return;
}
cout << "文件" << name << "保存完毕" << endl; fwrite(buf, length, 1, f);
fclose(f);
}int main()
{
OpenAndSavePic();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询