转载:https://blog.51cto.com/zgqwork/389804
说明:
1.需要添加头文件,以及配置工程属性才能运行(个人没有配置成功,但是放到项目里可以运行成功);
2.不要头文件 #include "atlp_w_picpath.h";
3.
GetEncoderClsidOwn(L"p_w_picpath/jpeg", &encoderClsid);
这里有问题,似乎没有这种格式。如果水印图片是png格式的,这里应该是
GetEncoderClsidOwn(L"image/png", &encoderClsid);
原代码:
1 #include "stdafx.h" 2 #include "gdiplus.h" 3 #include "atlp_w_picpath.h" 4 using namespace Gdiplus; 5 #include <string> 6 using namespace std; 7 GdiplusStartupInput gSDKdiplusStartupInput = 0; 8 ULONG_PTR gSDKdiplusToken = 0; 9 10 int GetEncoderClsidOwn(const WCHAR* format, CLSID* pClsid) 11 { 12 UINT num = 0; // number of p_w_picpath encoders 13 UINT size = 0; // size of the p_w_picpath encoder array in bytes 14 15 ImageCodecInfo* pImageCodecInfo = NULL; 16 17 GetImageEncodersSize(&num, &size); 18 if(size == 0) 19 return -1; // Failure 20 21 pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); 22 if(pImageCodecInfo == NULL) 23 return -1; // Failure 24 25 GetImageEncoders(num, size, pImageCodecInfo); 26 27 for(UINT j = 0; j < num; ++j) 28 { 29 if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) 30 { 31 *pClsid = pImageCodecInfo[j].Clsid; 32 free(pImageCodecInfo); 33 return j; // Success 34 } 35 } 36 37 free(pImageCodecInfo); 38 return -1; // Failure 39 } 40 41 void main() 42 43 { 44 45 GdiplusStartup(&gSDKdiplusToken, &gSDKdiplusStartupInput, NULL); 46 47 USES_CONVERSION; 48 FontFamily fontFamily(L"Arial"); 49 float fontSize = 12*(float)abs(2); 50 Font font(&fontFamily, fontSize, FontStyleBold, UnitPixel); 51 SolidBrush blackBrush(Color(0, 0, 0)); 52 PointF pointF(1, 1); 53 CLSID encoderClsid; 54 GetEncoderClsidOwn(L"p_w_picpath/jpeg", &encoderClsid); 55 string filename; 56 WCHAR wfilename[MAX_PATH]; 57 for(int i=0;i<20;i++) 58 { 59 filename = "out_"; 60 char str[4]; 61 itoa(i, str, 10); 62 filename = filename+str; 63 filename=filename+".jpg"; 64 memset(wfilename,0,MAX_PATH); 65 wcscpy(wfilename, T2W(filename.c_str()) ); 66 Bitmap bmp(L"sample.jpg"); 67 Graphics g(&bmp); 68 g.DrawString(wfilename, -1, &font, pointF, &blackBrush); 69 bmp.Save(wfilename,&encoderClsid); 70 printf("%d ",i); 71 } 72 73 GdiplusShutdown(gSDKdiplusToken); 74 75 }