最近需要缩放N多图片,
找遍了互联网也没有找到方便使用的批量缩放工具..
趁着周末写一个练手..
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
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 &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翻转工具. #include
批量下载网站图片的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图片质量足够好; ...
随机推荐
MVC概念性的内容
MVC: 是一个缩写(model + view + control), Model:是一些类文件, 功能:负责增删改查, 负责跟数据库打交道 (把数据存入到数据库: 从数据库把数据读 ...
grep、egrep、fgrep
grep: global search regular expression and printing
VC++调试说明
目录 第1章调试说明 1 1.1 调试设置 1 1.2 跟踪代码 2 1.3 断点 2 第2章模块生命周期 4 2.1 exe模块 4 2.2 dll模块 5 ...
SQL语句 (一)
1 SQL语句分类: 数据查询语句(DQL): SELECT 数据操纵语言 (DML): INSERT.UPDATE.DELETE 数据定义语言 (DDL): 数据控制语言 (DCL): GRANT. ...
Spring事件和监听器
Application下抽象子类ApplicationContextEvent的下面有4个已经实现好的事件 ContextClosedEvent(容器关闭时) ContextRefreshedEven ...
Linux常见命令快捷方式
命令行编辑的辅助操作: Tab健:自动补齐 Ctrl +U :清空至首行 Ctrl +K: 清空至尾行 Ctrl +L:(或者clear) 清屏 Ctrl +C: 取消执行命令 获取帮助命令: 内 ...
JAVA虚拟机是?为什么称作是“平台无关的语言”?
Java虚拟机(Java Virtual Machine)简称JVM ,它是抽象化的计算机,有自己完善的硬体架构,如处理器.堆栈.寄存器等,还具有相应的指令系统.JVM屏蔽了与具体操作系统平台相关的信 ...
java 正则表达式验证邮箱格式是否合规 以及 正则表达式元字符
package com.ykmimi.testtest; /** * 测试邮箱地址是否合规 * @author ukyor * */ public class EmailTest { public s ...
elasticsearch6.5集群环境搭建的一些坑
都说el配置很简单,确实比solr简单多了,不用手动配置一大堆,不过第一次配置也不轻松,因为马虎老是漏掉了许多地方 配置一个半小时才启动成功: 这里主要记录一下一些遇到的坑: 一 不能用root启动, ...
LINUX设备驱动程序笔记(五)中断处理
中断处理流程例如以下: 1.发生中断时,CPU运行异常向量vector_irq的代码. 2.在vector_irq里面.终于会调用中断处理的总入口函数asm_do_IRQ ...