See an ASP written to read the image file length, width of the program, feeling a little meaning, so with C + + also wrote a.
#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;
}