没有躲过的坑

编程入门 行业动态 更新时间:2024-10-10 14:31:49

没有躲过的坑

没有躲过的坑

有时候问题总是被想的过于复杂!

简单说就是读取一张图片,然后得到这个图片的width和height。

首先,用到的库没有Image这个控件,所以不能从控件获得图片的高和宽。

于是GOOGLE了一个算法,稍作修改,对于不同的类型有不同的计算方法,上代码吧:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{string fname = "D:\\暴走.png";ifstream ffin(fname, std::ios::binary);if (!ffin) {cout << "Can not open this file." << endl;return 0;}int image_type = 0;char c = fname.at(fname.length() - 1);char c2 = fname.at(fname.length() - 3);if ((c == 'f') && (c2 == 'g')) {  // file extension name is gif image_type = 1;}else if ((c == 'g') && (c2 == 'j')) { // file extension name is jpgimage_type = 2;}else if ((c == 'g') && (c2 == 'p')) { // file extension name is pngimage_type = 3;}else if ((c == 'p') && (c2 == 'b')) { // file extension name is bmpimage_type = 4;}char s1[2] = { 0 };char s2[2] = { 0 };long m_Width = 0;long m_Height = 0;switch (image_type){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:  // jpgffin.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:   // pngffin.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();cout << "width:" << m_Width  << endl;cout << "height:" << m_Height << endl;
}

上述代码的优点就是,不用读取整个图片的大小,效率是很高的。

但是,有一个很大的坑,就是无法计算出小于24kb的图片,这不是坑爹的吗?

咨询了一下其他平台的做法,用了一个叫ImageMagic的库,用于图片的处理很强大。配置了好久,但是还是会报错。也许跟vs编译器的版本有关吧。况且为了一个小小的功能就引入一个库,未免有点小题大做了。

蓦然回首,CImage类正等着呢!

原来忘记了最基本的windows GDI了。

好吧,别忘了,引入头文件:

#include<iostream>
#include<atlimage.h>
int main()
{CImage m;std::wstring imgPath = _T("d:\\222.png");m.Load(imgPath.c_str());std::cout << m.GetWidth() << std::endl;return 0;
}

没有躲过的坑儿–我们忽略那些微小而基本的功能!

更多推荐

没有躲过的坑

本文发布于:2024-02-27 16:21:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!