Board logo

标题: 图片格式转换工具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"


原创代码:
  1. /*
  2. CONSOLE PICTURE FORMAT CONVERSION TOOL, COPYRIGHT@2017~2019 BY HAPPY, VERSION 1.0
  3. IMGCONV.EXE
  4. */
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <gdiplus.h>
  8. #pragma comment(lib, "GdiPlus.lib")
  9. //使用GDI+
  10. using namespace Gdiplus;
  11. //定义帮助说明
  12. #define HELP_INFORMATION "\
  13. -----------------------------------------------------------------\n\
  14. CONSOLE PICTURE FORMAT CONVERSION TOOL, VERSION 1.0\n\
  15. -----------------------------------------------------------------\n\
  16. imgconv [input] [output]\n\
  17. -----------------------------------------------------------------\n\
  18.         Support bmp png gif jpg  jpeg tiff exif ico\n\
  19. -----------------------------------------------------------------\n\
  20. COPYRIGHT@2017~2019 BY HAPPY,2017-02-06"
  21. //转码宽字符
  22. WCHAR* L(const CHAR* str)
  23. {
  24. if(!str){return NULL;}
  25. int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, NULL, 0);
  26. WCHAR* wstr=(WCHAR*)malloc(sizeof(WCHAR)*wLen + 1);
  27. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstr, wLen);
  28. wstr[wLen]='\0';
  29. return wstr;
  30. }
  31. //获取编码器CLSID
  32. BOOL GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  33. {
  34. UINT j, n=0, s=0;
  35. ImageCodecInfo* pInfo=NULL;
  36. GetImageEncodersSize(&n, &s);
  37. if(s==0){return FALSE;}
  38. pInfo=(ImageCodecInfo*)(malloc(s));
  39. if(pInfo==NULL){return FALSE;}
  40. GetImageEncoders(n, s, pInfo);
  41. for(j=0; j<n; j++){
  42. if(wcscmp(pInfo[j].MimeType, format)==0){
  43. *pClsid=pInfo[j].Clsid;
  44. free(pInfo);
  45. return TRUE;
  46. }   
  47. }
  48. free(pInfo);
  49. return FALSE;
  50. }
  51. //主函数入口
  52. int main(int argc, char** argv)
  53. {
  54. if(argc!=3){
  55. //输入错误,则抛出使用说明
  56. fputs(HELP_INFORMATION, stderr);
  57. exit(1);
  58. }
  59. CHAR *p=argv[2], *formatSTR=NULL;
  60. while(*p!='\0'){p++;}
  61. int   l=p-argv[2];
  62. if(l<4){
  63. //扩展名过于短小
  64. fputs("The extension is too short", stderr);
  65. exit(1);
  66. }
  67. if(argv[2][l-4]=='.'){
  68. switch(argv[2][l-3]){
  69. case 'b':
  70. formatSTR="image/bmp";
  71. break;
  72. case 'p':
  73. formatSTR="image/png";
  74. break;
  75. case 'g':
  76. formatSTR="image/gif";
  77. break;
  78. case 'i':
  79. formatSTR="image/ico";
  80. break;
  81. case 'j':
  82. formatSTR="image/jpeg";
  83. break;
  84. default:
  85. //错误的图片转换格式
  86. fputs("Wrong picture conversion format", stderr);
  87. exit(1);
  88. }
  89. }else if(argv[2][l-5]=='.'){
  90. switch(argv[2][l-4]){
  91. case 'j':
  92. formatSTR="image/jpeg";
  93. break;
  94. case 't':
  95. formatSTR="image/tiff";
  96. break;
  97. case 'e':
  98. formatSTR="image/exif";
  99. break;
  100. default:
  101. //错误的图片转换格式
  102. fputs("Wrong picture conversion format", stderr);
  103. exit(1);
  104. }
  105. }else{
  106. //错误的图片转换格式
  107. fputs("Wrong picture conversion format", stderr);
  108. exit(1);
  109. }
  110. //初始化GDI+
  111. ULONG_PTR gdipludToken;
  112. GdiplusStartupInput gdiplusInput;
  113. GdiplusStartup(&gdipludToken,&gdiplusInput,NULL);
  114. //读取图像
  115. Image* srcIMG=Image::FromFile(L(argv[1]));
  116. CLSID  clsid;
  117. if(GetEncoderClsid(L(formatSTR), &clsid)){
  118. //保存位图
  119. srcIMG->Save(L(argv[2]), &clsid, NULL);
  120. }else{
  121. //无法转换图片格式
  122. fputs("Can't conversion format", stderr);
  123. exit(1);
  124. }
  125. //关闭GDI+
  126. GdiplusShutdown(gdipludToken);
  127. return 0;
  128. }
复制代码

作者: lancer    时间: 2022-10-10 15:39

不支持最新的webp格式,希望能加入更多的支持格式




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2