GDI+添加图片水印

bool GetCodecClsid(LPCTSTR lpstrFormat, CLSID &clsid)
{

 

 
       UINT nNum = 0, nSize = 0;
       Gdiplus::GetImageEncodersSize(&nNum, &nSize) == Gdiplus::Ok;

 
       Gdiplus::ImageCodecInfo *pInfo = (Gdiplus::ImageCodecInfo *)malloc(nSize);

 

 
       if(Gdiplus::GetImageEncoders(nNum, nSize, pInfo) != Gdiplus::Ok)
       {
             free(pInfo);
       }

 
       LPCWSTR lpstr = lpstrFormat;

 
       for(int nIndex = 0; nIndex < nNum; ++nIndex)
       {
             if(wcscmp(pInfo[nIndex].MimeType, lpstr) == 0)
             {
                   clsid = pInfo[nIndex].Clsid;
                   return true;
             }
       }

 
       return false;
};

 

 
void DrawWatermark()
{
       USES_CONVERSION;
       int nPicWidth = 0;
       int nPicHeight = 0;
       ULONG_PTR m_gdiplusToken;
       Gdiplus::GdiplusStartupInput gdiplusStartupInput;
       if ( Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Ok )
       {
             return ;
       }

 

 

 

 
       Gdiplus::Bitmap oOldBitMap(A2W("Winter.jpg"));
        printf("%d\n",oOldBitMap.GetHeight());

 
//新画布
       Gdiplus::Bitmap bitmap(oOldBitMap.GetWidth (), oOldBitMap.GetHeight ());
       bitmap.SetResolution (oOldBitMap.GetHorizontalResolution (), oOldBitMap.GetVerticalResolution ());
      
       Gdiplus::Graphics graphics(&bitmap);
       graphics.SetSmoothingMode (SmoothingModeAntiAlias);
       graphics.SetTextRenderingHint (TextRenderingHintAntiAli as);

 

 
       Gdiplus::Bitmap oWatermark(A2W("paipai_logo_80.png"));
     //Image oWatermark(A2W("4.jpg"));
     // Gdiplus::Bitmap oWatermark(A2W("paipai_logo.png"));
       printf("%d",oWatermark.GetHeight());
       int nLocX = 0;
       int nLocY = 0;
       int nmode = oWatermark.GetLastStatus ();
      
       // 
       graphics.DrawImage(&oOldBitMap, 0, 0, oOldBitMap.GetWidth (), oOldBitMap.GetHeight ()); 
      
      
       int nStatus = graphics.DrawImage (&oWatermark,Gdiplus::Rect(0, 0,253, 53), 0, 0, 253, 53, Gdiplus::UnitPixel);
       graphics.DrawImage (&oWatermark,253,60, 0, 0, 253, 53, Gdiplus::UnitPixel);
       graphics.DrawImage (&oWatermark,253,100, 0, 0, 253, 53, Gdiplus::UnitPixel);

 
//            int nStatus = graphics.DrawImage (&oWatermark,0,0);
       //oOldBitMap.Save(A2W("Winter.jpg"))
       CLSID pngclsid = {0};
       GetCodecClsid(_T("image/jpeg"), pngclsid);

 
//      oOldBitMap.Save(A2W("Winter1.png"), &pngclsid);
  //   oWatermark.Save(A2W("logo.png"), &pngclsid);

 
       delete &oOldBitMap;

 
       if( PathFileExists( A2W("Winter.jpg") ))
       {
             if(GetFileAttributes(A2W("Winter.jpg")) & (~FILE_ATTRIBUTE_NORMAL))
             {
                   SetFileAttributes(A2W("Winter.jpg"), FILE_ATTRIBUTE_NORMAL);
             }
       if( !DeleteFile(A2W("Winter.jpg")) )
             perror( "Could not delete Winter.jpg" );
       else
             printf( "Deleted 'Winter.jpg'\n" );
       }
       bitmap.Save (A2W("Winter.jpg"), &pngclsid);

 
};

本文出自 “会飞de树(往github迁移中)” 博客,转载请与作者联系!

你可能感兴趣的:(职场,休闲,图片水印)