标题: 图片格式转换工具imgconv.exe [打印本页]
作者: happy886rr 时间: 2017-2-6 18:30 标题: 图片格式转换工具imgconv.exe
IMGCONV.EXE
摘要:
==========================================================
命令行图片格式转换工具,支持bmp、png、gif、jpg、jpeg、tiff、exif、ico等多种图片格式互转。
==========================================================
链接: https://pan.baidu.com/s/158gWUQQYytCUo5ULGXCROg?pwd=fp8h
用法:
-----------------------------------------------------------------------------
imgconv [input] [output]
-----------------------------------------------------------------------------
示例:
-----------------------------------------------------------------------------
imgconv test.ico test.png //将ico图标转为透明png
-----------------------------------------------------------------------------
备注:
-----------------------------------------------------------------------------
CONSOLE PICTURE FORMAT CONVERSION TOOL, VERSION 1.0
-----------------------------------------------------------------------------
imgconv [input] [output]
-----------------------------------------------------------------------------
Support bmp png gif jpg jpeg tiff exif ico
-----------------------------------------------------------------------------
COPYRIGHT@2017~2019 BY HAPPY,2017-02-06"
原创代码:- /*
- CONSOLE PICTURE FORMAT CONVERSION TOOL, COPYRIGHT@2017~2019 BY HAPPY, VERSION 1.0
- IMGCONV.EXE
- */
-
- #include <stdio.h>
- #include <windows.h>
- #include <gdiplus.h>
- #pragma comment(lib, "GdiPlus.lib")
- //使用GDI+
- using namespace Gdiplus;
-
- //定义帮助说明
- #define HELP_INFORMATION "\
- -----------------------------------------------------------------\n\
- CONSOLE PICTURE FORMAT CONVERSION TOOL, VERSION 1.0\n\
- -----------------------------------------------------------------\n\
- imgconv [input] [output]\n\
- -----------------------------------------------------------------\n\
- Support bmp png gif jpg jpeg tiff exif ico\n\
- -----------------------------------------------------------------\n\
- COPYRIGHT@2017~2019 BY HAPPY,2017-02-06"
-
- //转码宽字符
- WCHAR* L(const CHAR* str)
- {
- if(!str){return NULL;}
- int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, NULL, 0);
- WCHAR* wstr=(WCHAR*)malloc(sizeof(WCHAR)*wLen + 1);
- MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstr, wLen);
- wstr[wLen]='\0';
- return wstr;
- }
-
- //获取编码器CLSID
- BOOL GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT j, n=0, s=0;
- ImageCodecInfo* pInfo=NULL;
- GetImageEncodersSize(&n, &s);
- if(s==0){return FALSE;}
- pInfo=(ImageCodecInfo*)(malloc(s));
- if(pInfo==NULL){return FALSE;}
- GetImageEncoders(n, s, pInfo);
- for(j=0; j<n; j++){
- if(wcscmp(pInfo[j].MimeType, format)==0){
- *pClsid=pInfo[j].Clsid;
- free(pInfo);
- return TRUE;
- }
- }
- free(pInfo);
- return FALSE;
- }
-
- //主函数入口
- int main(int argc, char** argv)
- {
- if(argc!=3){
- //输入错误,则抛出使用说明
- fputs(HELP_INFORMATION, stderr);
- exit(1);
- }
- CHAR *p=argv[2], *formatSTR=NULL;
- while(*p!='\0'){p++;}
- int l=p-argv[2];
- if(l<4){
- //扩展名过于短小
- fputs("The extension is too short", stderr);
- exit(1);
- }
- if(argv[2][l-4]=='.'){
- switch(argv[2][l-3]){
- case 'b':
- formatSTR="image/bmp";
- break;
- case 'p':
- formatSTR="image/png";
- break;
- case 'g':
- formatSTR="image/gif";
- break;
- case 'i':
- formatSTR="image/ico";
- break;
- case 'j':
- formatSTR="image/jpeg";
- break;
- default:
- //错误的图片转换格式
- fputs("Wrong picture conversion format", stderr);
- exit(1);
- }
- }else if(argv[2][l-5]=='.'){
- switch(argv[2][l-4]){
- case 'j':
- formatSTR="image/jpeg";
- break;
- case 't':
- formatSTR="image/tiff";
- break;
- case 'e':
- formatSTR="image/exif";
- break;
- default:
- //错误的图片转换格式
- fputs("Wrong picture conversion format", stderr);
- exit(1);
- }
- }else{
- //错误的图片转换格式
- fputs("Wrong picture conversion format", stderr);
- exit(1);
- }
-
- //初始化GDI+
- ULONG_PTR gdipludToken;
- GdiplusStartupInput gdiplusInput;
- GdiplusStartup(&gdipludToken,&gdiplusInput,NULL);
- //读取图像
- Image* srcIMG=Image::FromFile(L(argv[1]));
- CLSID clsid;
- if(GetEncoderClsid(L(formatSTR), &clsid)){
- //保存位图
- srcIMG->Save(L(argv[2]), &clsid, NULL);
- }else{
- //无法转换图片格式
- fputs("Can't conversion format", stderr);
- exit(1);
- }
- //关闭GDI+
- GdiplusShutdown(gdipludToken);
- return 0;
- }
复制代码
作者: lancer 时间: 2022-10-10 15:39
不支持最新的webp格式,希望能加入更多的支持格式
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |