最近需要缩放N多图片,

找遍了互联网也没有找到方便使用的批量缩放工具..

趁着周末写一个练手..

 #include <iostream>
 #include <vector>
 #include <list>
 #include <string>
 #include <iterator>
 #include <functional>
 #include <memory>
 #include <fstream>
 #include <sstream>

 #include <Shlwapi.h>
 #include <Windows.h>
 #include <gdiplus.h>

 #pragma comment(lib, "gdiplus.lib")
 #pragma comment(lib, "Shlwapi.lib")

 inline std::wstring getDirectory(const std::wstring &fullname)
 {
     auto findIndex = fullname.find_last_of('\\');
     return findIndex != std::wstring::npos
         ? fullname.substr(, findIndex + ) : std::wstring();
 }

 inline std::wstring getFullName(const std::wstring &fullname)
 {
     auto findIndex = fullname.find_last_of('\\');
     return findIndex != std::wstring::npos
         ? fullname.substr(findIndex + ) : std::wstring();
 }

 template <class T1, class T2>
 inline T1 parseTo(const T2 &t)
 {
     static std::wstringstream sstream;
     T1 r;
     sstream << t;
     sstream >> r;
     sstream.clear();
     return r;
 }

 CLSID findCLSID(const std::wstring &format)
 {
     CLSID clsid = {  };
     auto num = 0u;
     auto size = 0u;
     Gdiplus::GetImageEncodersSize(&num, &size);
     )
     {
         auto pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
         if (pImageCodecInfo != nullptr)
         {
             GetImageEncoders(num, size, pImageCodecInfo);

             ; j< num; ++j)
             {
                 if (format == pImageCodecInfo[j].MimeType)
                 {
                     clsid = pImageCodecInfo[j].Clsid;
                     break;
                 }
             }
             free(pImageCodecInfo);
         }
     }
     return clsid;
 }

 void forEachFileList(
     const std::wstring &fileListName,
     const std::function<void (const std::wstring &)> &handler)
 {
     std::wifstream ifile(fileListName);
     if (ifile)
     {
         std::wstring fileName;
         while (std::getline(ifile, fileName))
         {
             handler(fileName);
         }
         ifile.close();
     }
 }

 int createSaveDirector(const std::wstring &director)
 {
     auto result = true;
     if (!PathFileExists(director.c_str()))
     {
         result = CreateDirectory(director.c_str(), nullptr) != ;
     }
     return result;
 }

 void saveImage(Gdiplus::Bitmap *pImage, const CLSID &clsid, const std::wstring &saveFile)
 {
     auto director = getDirectory(saveFile);
     director.append(L"\\save\\");
     if (createSaveDirector(director))
     {
         director.append(getFullName(saveFile));
         std::wcout
             << L"save image file: "
             << director << std::endl;
         pImage->Save(director.c_str(), &clsid);
     }
 }

 void scaleImage(const std::wstring &fileName, const CLSID &clsid, float scalex, float scaley)
 {
     Gdiplus::Bitmap image(fileName.c_str());
     if (image.GetLastStatus() == Gdiplus::Ok)
     {
         auto srcWidth= image.GetWidth();
         auto srcHeight = image.GetHeight();
         auto dstWidth = srcWidth * scalex;
         auto dstHeight = srcHeight * scaley;
         Gdiplus::Bitmap newImage((u_int)dstWidth, (u_int)dstHeight);
         Gdiplus::Graphics graphics(&newImage);
         graphics.DrawImage(&image, 0u, 0u, dstWidth, dstHeight);
         saveImage(&newImage, clsid, fileName);
     }
 }

 void run(const std::wstring &fileList, float scalex, float scaley)
 {
     u_long token;
     Gdiplus::GdiplusStartupInput input;
     Gdiplus::GdiplusStartup(&token, &input, nullptr);
     {
         auto clsid = findCLSID(L"image/png");
          || clsid.Data2 !=  || clsid.Data3 != )
         {
             forEachFileList(
                 fileList,
                 std::bind(scaleImage, std::placeholders::_1, clsid, scalex, scaley));
         }
     }
     Gdiplus::GdiplusShutdown(token);
 }

 int wmain(int argn, wchar_t *argc[])
 {
     std::locale(std::locale::global(std::locale("chs")));
     std::wcout << argn << std::endl;
     )
     {
         std::wcout
             << L"参数格式: "
             << L"imglist.txt scalex scaley"
             << std::endl;
     }
     else
     {
         run(argc[],
             parseTo<]),
             parseTo<]));
     }
     std::wcout << L"run done." << std::endl;
     std::wcin.get();
     ;
 }

下载地址

使用说明:

  在控制台下运行即可.

运行需要三个参数分别是: 需要缩放的图片路径列表, x缩放之, y缩放值.

第一个参数是一个包含了需要被缩放的图片路径列表, 是纯文本文件, 可以用cmd命令来生成:

for /r %%i in(.,*) do echo %%i>>.\filelist.txt

路径列表图

缩放0.1效果图

批量缩放PNG图片.的更多相关文章

  1. 批量翻转PNG图片

    用了好几个软件都不好用. 要么不能翻转PNG, 要么翻转之后没有透明度了. 基本上全是图形界面, 要鼠标批量拖放. 所以, 还是自己动手, 写一个批量png翻转工具. #include <ios ...

  2. 批量下载网站图片的Python实用小工具

    定位 本文适合于熟悉Python编程且对互联网高清图片饶有兴趣的筒鞋.读完本文后,将学会如何使用Python库批量并发地抓取网页和下载图片资源.只要懂得如何安装Python库以及运行Python程序, ...

  3. Magnifier.js - 支持鼠标滚轮缩放的图片放大镜效果

    Magnifier.js 是一个 JavaScript 库,能够帮助你在图像上实现放大镜效果,支持使用鼠标滚轮放大/缩小功能.放大的图像可以显示在镜头本身或它的外部容器中.Magnifier.js 使 ...

  4. jQuery实现等比例缩放大图片

      在布局页面时,有时会遇到大图片将页面容器“撑破”的情况,尤其是加载外链图片(通常是通过采集的外站的图片).那么本文将为您讲述使用jQuery如何按比例缩放大图片,让大图片自适应页面布局. 通常我们 ...

  5. Android调用相册拍照控件实现系统控件缩放切割图片

    android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 package cn.m15.test; import java.io.By ...

  6. Android实现支持缩放平移图片

    本文主要用到了以下知识点 Matrix GestureDetector 能够捕捉到长按.双击 ScaleGestureDetector 用于检测缩放的手势 自由的缩放 需求:当图片加载时,将图片在屏幕 ...

  7. Android 调用相册 拍照 实现系统控件缩放 切割图片

    android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家. package cn.m15.test; import java.io.B ...

  8. Zencart批量删除无图片产品

    Zencart批量删除无图片产品 2012-04-23 07:26:18|  分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0   ...

  9. LODOP用ADD_PRINT_IMAGE语句缩放打印图片

    LODOP提高输出图片质量的方法:1.用ADD_PRINT_IMAGE语句打印图片,而且img元素的width和height属性要去掉或者设置足够大,这样就可以让下载引擎传给Lodop图片质量足够好; ...

随机推荐

  1. php 正则校验是否是域名

    /** * @description 匹配 * t.cn 正确 * t-.cn 错误 * tt.cn正确 * -t.cn 错误 * t-t.cn 正确 * tst-test-tst.cn 正确 * t ...

  2. python爬虫学习(2)__抓取糗百段子,与存入mysql数据库

    import pymysql import requests from bs4 import BeautifulSoup#pymysql链接数据库 conn=pymysql.connect(host= ...

  3. hdu 4758 Walk Through Squares

    AC自动机+DP.想了很久都没想出来...据说是一道很模板的自动机dp...原来自动机还可以这么跑啊...我们先用两个字符串建自动机,然后就是建一个满足能够从左上角到右下角的新串,这样我们直接从自动机 ...

  4. HDU 5750 Dertouzos

    Dertouzos Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  5. ASP.NET- Web.Config配置大文件上传

    在web.config中的<system.web></system.web>内加入如下代码: <httpRuntime executionTimeout="60 ...

  6. 3proxy代理软件文档说明

    官方英文原版说明:http://www.3proxy.ru/howtoe.asp 配置文件的简要说明:如果你的英文理解力好,可以试着研究一下他的手册. 以实例说明吧 nscache 65536域名解析 ...

  7. jquery-data的三种用法

    1.jquery-data的用处 jQuery-data主要是用来存储数据,帮助普通对象或者jQuery对象来存储数据,其实如果单纯的储存dom的单一的属性,用attr自定义属性足够了:如果存储多个键 ...

  8. java获得项目绝对路径

    在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...

  9. MVC-控制器向View传值的三种方法

    1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResult Index() { DateTime date = DateTime.Now; return ...

  10. saveFileDialog

    saveFileDialog1.ShowDialog saveFileDialog.FileName 设置的时候是一个字符串. 如: 新建 RTF 文档.rtf 获得的时候 则为一个完整的路径. 如: ...