• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi 图片加水印源代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
unit UWaterMark;

interface

uses

{$IFNDEF DELPHIXE2ANDUP}

   windows,SysUtils,classes,graphics,Gdiplus;

 {$ELSE}

   winapi.windows, System.SysUtils,System.Classes,Vcl.Graphics,Gdiplus;

 {$ENDIF}

 type  TWaterMarker=class

      private
         fSourcePic,fWaterMarkPic,fCopyRight:string;
         fResolution:Single;

         Photo: TGpImage;
         PhWidth: Integer;
         PhHeight: Integer;
         Watermark: TGpImage;
         WmWidth: Integer;
         WmHeight: Integer;
         Bmp: TGpBitmap;

         procedure setSourcePic(value:string);
         procedure setWaterMarkPic(value:string);
         procedure setCopyRight(value:string);
         procedure  setResolution(value:single) ;

      public
        constructor  create(sourcePic:string='';waterMarkPic: string='');
        destructor   destroy();override;

        procedure  prepare();
        procedure  Render(bShowCopyRight: boolean;fontSize:single=20);
        procedure  save;
        procedure  Show(Handle: HDC);

      published
          property  SourcePic:string read fSourcePic write setSourcePic;
          property  WaterMarkPic:string read fWaterMarkPic write setWaterMarkPic;
          property  CopyRight:string read fCopyRight write setCopyRight;
          property  Resolution:single read fResolution write setResolution;


 end;



implementation
    uses GdipTypes;


{ TWaterMarker }

constructor TWaterMarker.create(sourcePic:string='';waterMarkPic: string='');
begin
  fSourcePic:=sourcePic;
  fWaterMarkPic:=waterMarkPic;
  fCopyRight := 'Copyright ? WaterMark ';
  fResolution:=72;  //分辨率为72
end;

procedure  TWaterMarker.prepare();
begin
  if not fileExists(fSourcePic) then Exit;
  if not fileExists(fWaterMarkPic) then Exit;

    // 读取原始图片
  Photo := TGpImage.Create(sourcePic);
  PhWidth := Photo.Width;
  PhHeight := Photo.Height;
  // 读取水印图片
  Watermark := TGpImage.Create(waterMarkPic);
  WmWidth := Watermark.Width;
  WmHeight := Watermark.Height;
  // 建立一个新的位图
  Bmp := TGpBitmap.Create(PhWidth, PhHeight, pf32bppArgb);
  Bmp.SetResolution(fResolution, fResolution);
end;

destructor TWaterMarker.destroy;
begin
   if assigned(Photo) then Photo.Free;
   if assigned(Watermark) then Watermark.Free;
   if assigned(Bmp) then Bmp.Free;
  inherited;
end;

procedure TWaterMarker.Render(bShowCopyRight: boolean;fontSize:single=20);
const
  ColorMatrix: TColorMatrix =
  (
    (1.0, 0.0, 0.0, 0.0, 0.0),
    (0.0, 1.0, 0.0, 0.0, 0.0),
    (0.0, 0.0, 1.0, 0.0, 0.0),
    (0.0, 0.0, 0.0, 0.5, 0.0),
    (0.0, 0.0, 0.0, 0.0, 1.0)
  );
var
  gp: TGpGraphics;
  imageAttr: TGpImageAttributes;
  strFormat: TGpStringFormat;
  font: TGpFont;
  x, y: Single;
begin
   // 建立新位图的画布,并设置图像显示质量和文本显示质量
  if  (Not assigned(Bmp)) or (Bmp=nil) then Exit;

  gp := TGpGraphics.Create(Bmp);
  gp.SmoothingMode := smAntiAlias;
  gp.TextRenderingHint := thAntiAlias;
  // 在画布上画原始图片
  gp.DrawImage(Photo, GpRect(0, 0, PhWidth, PhHeight),
               0, 0, PhWidth, PhHeight, utPixel);
  // 建立图像显示辅助类
  imageAttr := TGpImageAttributes.Create;
  // 设置透明颜色为水印图片四角的底色,水印图显示为圆角图片
  imageAttr.SetColorKey($ff00ff00, $ff00ff00, ctBitmap);
  // 设置水印图片不透明度为0.3
  imageAttr.SetColorMatrix(ColorMatrix, cfDefault, ctBitmap);

  // 在画布右下角画水印图
  gp.DrawImage(Watermark, GpRect(PhWidth - WmWidth - 10,PhHeight-WmHeight- 10, WmWidth, WmHeight), 0, 0, WmWidth, WmHeight, utPixel, imageAttr);


  if bShowCopyRight then
  begin
      // 设置文本字体和显示格式
      font := TGpFont.Create('arial', fontSize, [fsBold]);
      strFormat := TGpStringFormat.Create;
      strFormat.Alignment := saCenter;
      // 在画布下方居中显示阴影文本
      x := PhWidth / 2;
      y := PhHeight - 36;
      gp.DrawString(fCopyRight, font, Brushs[$99000000], x + 1, y + 1, strFormat);
      gp.DrawString(fCopyRight, font, Brushs[$99ffffff], x, y, strFormat);

      font.Free;
      strFormat.Free;
  end;


  imageAttr.Free;
  gp.Free;


end;

procedure TWaterMarker.save;
var
  Clsid: TGUID;
  Parameters: TEncoderParameters;
  Quality: Integer;
begin

  if not fileexists(fSourcePic) then   Exit;
  if (not assigned(Bmp))  or (Bmp=nil) then Exit;

  // 设置图像品质编码参数
  Parameters.Count := 1;
  Parameters.Parameter[0].Guid := EncoderQuality;
  Parameters.Parameter[0].ValueType := EncoderParameterValueTypeLong;
  Parameters.Parameter[0].NumberOfValues := 1;
  // 设置参数的值:品质等级,最高为100,图像文件大小与品质成正比
  Quality := 100;
  Parameters.Parameter[0].Value := @Quality;

 if GetEncoderClsid('image/jpeg', Clsid) then
     Bmp.Save(changefileext(fSourcePic,'_watermark.jpg'), Clsid, @Parameters);

end;

procedure TWaterMarker.setCopyRight(value: string);
begin
  fCopyRight:=value;
end;

procedure TWaterMarker.setResolution(value: single);
begin
   fResolution:=value;
end;

procedure TWaterMarker.setSourcePic(value:string);
begin
    fSourcePic:=value;
end;

procedure TWaterMarker.setWaterMarkPic(value: string);
begin
   fWaterMarkPic:=value;
end;

procedure TWaterMarker.Show(Handle: HDC);
var
  g: TGpGraphics;
begin
 //将生成的水印效果图像直接显示到handle所在的画布上
   g := TGpGraphics.Create(Handle);
   g.TranslateTransform(0, 0);
   g.DrawImage(bmp, 0, 0, PhWidth, PhHeight);
   g.Free;
end;

end.
var WM: TWaterMarker;
begin
  //  WM:=TWaterMarker.create('images/my.jpg','images/watermark.jpg');
    WM:=TWaterMarker.create();
    WM.SourcePic:='images/my.jpg';
    WM.WaterMarkPic:='images/watermark.jpg';
    WM.Resolution:=72;

    WM.prepare;
    WM.Render(true,30);
   // WM.save;
    WM.Show(self.Canvas.Handle);

    WM.Free ;
    WM:=nil;
    showmessage('ok');
end;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ConvertUTCstringtoTDatetimeinDelphi发布时间:2022-07-18
下一篇:
php、asp、java、c#、delphi之间的语言对照发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap