最近需要缩放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. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  2. UVA 11802 All Your Bases Belong to Us

    题意:这个题题意个人觉得蛮难懂的....意思就是求,把十进制下的n!转化成m进制,末位有且仅有k个连续的0.告诉n和k,求满足题意的m有多少个. 1<= k <= 10^15,n < ...

  3. 选择服务器OS标准

    稳定性.可靠性.兼容性.高效率.可持续,五大标准; recommend always using the stable version for production environments http ...

  4. typeahead使用配置参数。

    示例代码: var suggestion_source = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace( ...

  5. 记录:Ubuntu下配置Eclipse

    >>下载Eclipse. 下载链接:http://www.eclipse.org/downloads/ 我选择了64bit的版本 >>安装Eclipse. 解压文件: tar ...

  6. Python抓取淘宝IP地址数据

    def fetch(ip): url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip result = [] try: response ...

  7. Cloud Foundry中gorouter对StickySession的支持

    Cloud Foundry作为业界出众的PaaS平台,在应用的可扩展性方面做得很优秀. 详细来讲,在一个应用须要横向伸展的时候,Cloud Foundry能够轻松地帮助用户做好伸展工作,也就是创建出一 ...

  8. [D3] 7. Quantitative Scales

    # Quantitative Scales var colorScale = d3.scale.quantile() .domain([d3.max(dataset) / 4, d3.max(data ...

  9. &&与&

    if((2>1)&&(4>3))System.out.printf("两边都是true"); else   System.out.println(&qu ...

  10. NDK-r7以上版本部署方法

    一.关于NDK: NDK全称:Native Development Kit. 1.NDK是一系列工具的集合. NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和jav ...