als mimetype 圖片格式轉換 bmp 轉換 png cin pfile nullptr

 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 }

圖片格式轉換