码迷,mamicode.com
首页 > 其他好文 > 详细

图片格式转换

时间:2019-01-11 18:07:50      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:mime   return   ptr   bsp   let   from   ret   col   img   

 1 void Bmp2Png(LPCWSTR szBmpFile, LPCWSTR szPngFile)
 2 {
 3     Image* image = nullptr;
 4     CLSID clsid;
 5 
 6     image = Image::FromFile(szBmpFile, TRUE);
 7 
 8     if (!image)
 9     {
10         printf("failed to call FromFile\n");
11     }
12 
13     if (!GetEncodeCLSID(L"image/png", &clsid))
14     {
15         printf("failed to call GetEncodeCLSID\n");
16     }
17     
18     if (image->Save(szPngFile, &clsid))
19     {
20         printf("failed to call Save\n");
21     }
22 
23     delete image;
24 }
25 
26 void Pgn2Bmp(LPCWSTR szPngFile, LPCWSTR szImgFile)
27 {
28     Image* image = nullptr;
29     CLSID clsid;
30 
31     image = Image::FromFile(szPngFile, TRUE);
32 
33     if (!image)
34     {
35         printf("failed to call FromFile\n");
36     }
37 
38     if (!GetEncodeCLSID(L"image/png", &clsid))
39     {
40         printf("failed to call GetEncodeCLSID\n");
41     }
42 
43     if (image->Save(szImgFile, &clsid))
44     {
45         printf("failed to call Save\n");
46     }
47 
48     delete image;
49 }
50 
51 bool GetEncodeCLSID(LPCWSTR szMimeType, CLSID* clsid)
52 {
53     UINT num = 0, size = 0;
54     ImageCodecInfo* pImageCodecInfo = NULL;
55     GetImageEncodersSize(&num, &size);
56     if (size == 0)
57     {
58         return FALSE;
59     }
60 
61     pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
62     if (pImageCodecInfo == NULL)
63     {
64         return FALSE;
65     }
66 
67     GetImageEncoders(num, size, pImageCodecInfo);
68 
69     BOOL bfound = FALSE;
70     for (UINT i = 0; !bfound && i < num; i++)
71     {
72         if (_wcsicmp(pImageCodecInfo[i].MimeType, szMimeType) == 0)
73         {
74             *clsid = pImageCodecInfo[i].Clsid;
75             bfound = TRUE;
76         }
77     }
78 
79     free(pImageCodecInfo);
80     return bfound;
81 }

 

图片格式转换

标签:mime   return   ptr   bsp   let   from   ret   col   img   

原文地址:https://www.cnblogs.com/endenvor/p/10256361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!