C++實現讀取圖片長度和寬度_C 語言

來源:互聯網
上載者:User

看到一個用ASP寫的讀取圖片檔案的長度、寬度的程式,感覺有點意思,於是用C++也寫了一個。

#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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.