给图片添加图片水印 博客分类: wxwidgets编程 wxwidgets水印
程序员文章站
2024-03-25 21:06:16
...
写在开始
不能用代码同时实现半透明和不规则形状的水印,只能选其一,有哪位高手指点一下不胜感激
此例中的半透明是在Fireworks中处理图片实现,不是用代码实现的
#include <wx/dcmemory.h> #include <wx/dcclient.h> #include <wx/dc.h> enum Alignment { aLeftTop=0, aLeftBottom=1, aRightTop=2, aRightBottom=3, aCenter=4 }; void MarkImage(wxDC &dc,//设备上下文 wxString srcimg,//目标图片 wxString waterimg,//水印图片 wxString desimg,//已添加水印图片 int Percent=0,//透明度百分比 Alignment align=aLeftTop,//水印位置 int dx=0,int dy=0//边界距离 ) { wxImage img1,img2; img1.LoadFile(srcimg,wxBITMAP_TYPE_ANY); img2.LoadFile(waterimg,wxBITMAP_TYPE_ANY); // if(!img2.HasAlpha()) // { // img2.InitAlpha(); // } // unsigned char *alpha_plane = img2.GetAlpha(); // int alpha = (Percent*255)/100; // memset(alpha_plane, alpha, img2.GetWidth() * img2.GetHeight()); // img2.SetMask(); // img2.SetMaskColour(0,0,255); wxBitmap bmp1(img1); wxBitmap bmp2(img2); int w1=bmp1.GetWidth(); int h1=bmp1.GetHeight(); int w2=bmp2.GetWidth(); int h2=bmp2.GetHeight(); int x=dx,y=dy; switch(align) { case aLeftBottom: y=h1-h2-dy; break; case aRightTop: x=w1-w2-dx; break; case aRightBottom: x=w1-w2-dx; y=h1-h2-dy; break; case aCenter: x=(w1-w2)/2; y=(h1-h2)/2; default: break; } wxMemoryDC memDC1,memDC2; memDC1.SelectObject(bmp1); // memDC.DrawBitmap(bmp2, x,y, true); memDC2.SelectObject(bmp2); memDC1.Blit(x,y,bmp2.GetWidth(),bmp2.GetHeight(),&memDC2,0,0,wxCOPY,true); bmp1.SaveFile(desimg,wxBITMAP_TYPE_JPEG); memDC1.Clear(); memDC1.SelectObject(wxNullBitmap); memDC2.Clear(); memDC2.SelectObject(wxNullBitmap); } void WaterMarkDemoDialog::OnAbout(wxCommandEvent& event) { wxMemoryDC dc; MarkImage(dc,_T("1.jpg"),_T("shape.png"),_T("3.jpg"),50,aCenter,100,200); }