zoukankan      html  css  js  c++  java
  • 给图片添加水印

    转载: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 }
  • 相关阅读:
    AOP之PostSharp3MethodInterceptionAspect
    AOP之PostSharp6EventInterceptionAspect(事件异步调用)
    C# Winform获取路径
    C#生成唯一的字符串或者数字
    【电信增值业务学习笔记】1 初步学习
    【读书笔记】《产品经理手册》
    【协议学习】PPPoE学习文档
    【电信增值业务学习笔记】2 移动网络基本概念和组网结构
    【电信增值业务学习笔记】3 语音类增值业务
    【通信基础知识】白噪声、相关解调和相干解调
  • 原文地址:https://www.cnblogs.com/Toya/p/13731297.html
Copyright © 2011-2022 走看看