批量缩放PNG图片.
最近需要缩放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图片.的更多相关文章
- 批量翻转PNG图片
用了好几个软件都不好用. 要么不能翻转PNG, 要么翻转之后没有透明度了. 基本上全是图形界面, 要鼠标批量拖放. 所以, 还是自己动手, 写一个批量png翻转工具. #include <ios ...
- 批量下载网站图片的Python实用小工具
定位 本文适合于熟悉Python编程且对互联网高清图片饶有兴趣的筒鞋.读完本文后,将学会如何使用Python库批量并发地抓取网页和下载图片资源.只要懂得如何安装Python库以及运行Python程序, ...
- Magnifier.js - 支持鼠标滚轮缩放的图片放大镜效果
Magnifier.js 是一个 JavaScript 库,能够帮助你在图像上实现放大镜效果,支持使用鼠标滚轮放大/缩小功能.放大的图像可以显示在镜头本身或它的外部容器中.Magnifier.js 使 ...
- jQuery实现等比例缩放大图片
在布局页面时,有时会遇到大图片将页面容器“撑破”的情况,尤其是加载外链图片(通常是通过采集的外站的图片).那么本文将为您讲述使用jQuery如何按比例缩放大图片,让大图片自适应页面布局. 通常我们 ...
- Android调用相册拍照控件实现系统控件缩放切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 package cn.m15.test; import java.io.By ...
- Android实现支持缩放平移图片
本文主要用到了以下知识点 Matrix GestureDetector 能够捕捉到长按.双击 ScaleGestureDetector 用于检测缩放的手势 自由的缩放 需求:当图片加载时,将图片在屏幕 ...
- Android 调用相册 拍照 实现系统控件缩放 切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家. package cn.m15.test; import java.io.B ...
- Zencart批量删除无图片产品
Zencart批量删除无图片产品 2012-04-23 07:26:18| 分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0 ...
- LODOP用ADD_PRINT_IMAGE语句缩放打印图片
LODOP提高输出图片质量的方法:1.用ADD_PRINT_IMAGE语句打印图片,而且img元素的width和height属性要去掉或者设置足够大,这样就可以让下载引擎传给Lodop图片质量足够好; ...
随机推荐
- Semi-definite programming优化工具
半正定优化工具(SDPLR) SDPLR 是一个求解大规模半正定规划问题的C语言包.具体使用方法参见: http://dollar.biz.uiowa.edu/~sburer/pmwiki/pmwik ...
- jQuery 参考手册 - 效果
(speed可选:规定动画的速度.默认是 "normal",可能的值:毫秒(比如 1500)"slow""normal""fast ...
- Hadoop 配置好hive,第一次在conf能进入,第二次就不行了,怎么办?
问题描述: 在 Hadoop 配置好 hive 数据仓库,在conf目录下通过hive命令进入hive数据仓库,非常顺利. 但关闭终端,第二次按这种方式却显示,无次命令. 怎么办? 解决办法: 在h ...
- CMake 入门实战 | HaHack
CMake 入门实战 | HaHack undefined
- PHP 遍历文件目录
/********************** 一个简单的目录递归函数 第一种实现办法:用dir返回对象 ***********************/ function tree($directo ...
- weblogic开发模式与生产模式介绍
weblogic开发模式与生产模式介绍 开发模式:该模式启用自动部署 生产模式:该模式关闭自动部署 weblogic server 三种部署方法:自动部署.控制台部署.命令部署 自动部署:当其处于启用 ...
- java数据结构--线性结构
一.数据结构 数据结构由数据和结构两部分组成,就是将数据按照一定的结构组合起来,这样不同的组合方式有不同的效率,可根据需求选择不同的结构应用在相应在场景.数据结构大致 分为两类:线性结构(如数组,链表 ...
- SpringMVC接收页面表单参数(转)
作者:CN.programmer.Luxh 和java相关 一个普通的表单. 表单的代码如下: <%@ page language="java" contentType=&q ...
- JSP版(utf8编码)的Ueditor百度文章编辑器配置以及使用说明
二话不说,先上图: 我配置好的效果大致是这些功能:基本的文字编辑功能.图片上传功能.附件上传功能.百度/谷歌地图搜索截图.视/音频发布功能.这个插件是现今我用过觉得最舒服的编辑器,功能齐全强大,稍微修 ...
- Android_设备隐私获取,忽略6.0权限管理
1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...