看到一个用ASP写的读取图片文件的长度、宽度的程序,感觉有点意思,于是用C++也写了一个。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class CImage
{
private:
long m_Width;
long m_Height;
int get_extension(string fname);
public:
CImage()
{
m_Width = 0;
m_Height = 0;
};
void LoadImage(char* fname);
long get_width()
{
return m_Width;
};
long get_height()
{
return m_Height;
};
};
int CImage::get_extension(string fname)
{
char c = fname.at(fname.length()-1);
char c2 = fname.at(fname.length()-3);
if ((c == 'f') && (c2 == 'g')){ // file extension name is gif
return 1;
}else if ((c == 'g') && (c2 == 'j')){ // file extension name is jpg
return 2;
}else if ((c == 'g') && (c2 == 'p')){ // file extension name is png
return 3;
}else if ((c == 'p') && (c2 == 'b')){ // file extension name is bmp
return 4;
}
return 0;
}
void CImage::LoadImage(char *fname)
{
m_Width = m_Height = 0;
ifstream ffin(fname, std::ios::binary);
if (!ffin){
cout<<"Can not open this file."<<endl;
return;
}
int result = get_extension(fname);
char s1[2] = {0}, s2[2] = {0};
switch(result)
{
case 1: // gif
ffin.seekg(6);
ffin.read(s1, 2);
ffin.read(s2, 2);
m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);
break;
case 2: // jpg
ffin.seekg(164);
ffin.read(s1, 2);
ffin.read(s2, 2);
m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);
break;
case 3: // png
ffin.seekg(17);
ffin.read(s1, 2);
ffin.seekg(2, std::ios::cur);
ffin.read(s2, 2);
m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);
break;
case 4: // bmp
ffin.seekg(18);
ffin.read(s1, 2);
ffin.seekg(2, std::ios::cur);
ffin.read(s2, 2);
m_Width = (unsigned int)(s1[1])<<8|(unsigned int)(s1[0]);
m_Height = (unsigned int)(s2[1])<<8|(unsigned int)(s2[0]);
break;
default:
cout<<"NO"<<endl;
break;
}
ffin.close();
};
int main(int argc, char *argv[])
{
if (argc < 2){
printf("usage: program imagefilename/n");
return 0;
}
CImage test;
test.LoadImage(argv[1]);
cout<<"width:"<<test.get_width()<<endl;
cout<<"height:"<<test.get_height()<<endl;
return 0;
}
|
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 29
-
2025-06-04 19
-
2025-06-04 30
-
2025-05-29 43
-
2025-06-04 84
热门评论

