图片格式转换
Posted endenvor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片格式转换相关的知识,希望对你有一定的参考价值。
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 "); 11 } 12 13 if (!GetEncodeCLSID(L"image/png", &clsid)) 14 { 15 printf("failed to call GetEncodeCLSID "); 16 } 17 18 if (image->Save(szPngFile, &clsid)) 19 { 20 printf("failed to call Save "); 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 "); 36 } 37 38 if (!GetEncodeCLSID(L"image/png", &clsid)) 39 { 40 printf("failed to call GetEncodeCLSID "); 41 } 42 43 if (image->Save(szImgFile, &clsid)) 44 { 45 printf("failed to call Save "); 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 }
以上是关于图片格式转换的主要内容,如果未能解决你的问题,请参考以下文章