C++实现读取图片的长度和宽度

感觉比较有用,这里记录一下:

文件如下info.cpp文件

#include 
#include 
#include 

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."<

编译成可执行文件:

 g++ info.cpp -o info

执行效果如下:

你可能感兴趣的:(c++)