图片格式转换

 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/bmp", &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 }

 

转载于:https://www.cnblogs.com/endenvor/p/10256361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值