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=);
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
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=);
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(, , PhWidth, PhHeight),
, , PhWidth, PhHeight, utPixel);
// 建立图像显示辅助类
imageAttr := TGpImageAttributes.Create;
// 设置透明颜色为水印图片四角的底色,水印图显示为圆角图片
imageAttr.SetColorKey($ff00ff00, $ff00ff00, ctBitmap);
// 设置水印图片不透明度为0.3
imageAttr.SetColorMatrix(ColorMatrix, cfDefault, ctBitmap); // 在画布右下角画水印图
gp.DrawImage(Watermark, GpRect(PhWidth - WmWidth - ,PhHeight-WmHeight- , WmWidth, WmHeight), , , WmWidth, WmHeight, utPixel, imageAttr); if bShowCopyRight then
begin
// 设置文本字体和显示格式
font := TGpFont.Create('arial', fontSize, [fsBold]);
strFormat := TGpStringFormat.Create;
strFormat.Alignment := saCenter;
// 在画布下方居中显示阴影文本
x := PhWidth / ;
y := PhHeight - ;
gp.DrawString(fCopyRight, font, Brushs[$], x + , y + , 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 := ;
Parameters.Parameter[].Guid := EncoderQuality;
Parameters.Parameter[].ValueType := EncoderParameterValueTypeLong;
Parameters.Parameter[].NumberOfValues := ;
// 设置参数的值:品质等级,最高为100,图像文件大小与品质成正比
Quality := ;
Parameters.Parameter[].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(, );
g.DrawImage(bmp, , , 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:=; WM.prepare;
WM.Render(true,);
// WM.save;
WM.Show(self.Canvas.Handle); WM.Free ;
WM:=nil;
showmessage('ok');
end;

delphi 图片加水印源代码的更多相关文章

  1. php 分享两种给图片加水印的方法

    本文章向码农们介绍 php 给图片加水印的两种方法,感兴趣的码农可以参考一下本文章的源代码. 方法一:PHP最简单的加水印方法 <?php // http://www.manongjc.com ...

  2. 火车头dede采集接口,图片加水印,远程图片本地化,远程无后缀的无图片本地化

    <?php /* [LocoySpider] (C)2005-2010 Lewell Inc. 火车采集器 DedeCMS 5.7 UTF8 文章发布接口 Update content: 图片加 ...

  3. thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...

  4. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  5. Java图片处理(二)图片加水印

    图片加水印,是通过图片重叠绘制实现的.实现代码如下: public static void press(String pressImg, String pressText, String target ...

  6. PHP给图片加水印

    <?php /** *图片加水印 *@param $srcImg 原图 *@param $waterImg 水印图片 *@param $savepath 保存路径 *@param $savena ...

  7. 如何用node.js批量给图片加水印

    上一篇我们讲了如何用node.js给图片加水印,但是只是给某一张图片加,并没有涉及到批量处理.这一篇,我们学习如果批量进行图片加水印处理. 一.准备工作: 首先,你要阅读完这篇文章:http://ww ...

  8. 使用 ImageEnView 给图片加水印,及建缩略图

    摘要: 使用 ImageEnView 给图片加水印,及建缩略图 {Power by hzqghost@21cn.com}unit CutWater; interface uses  Math,imag ...

  9. php对图片加水印--将图片先缩小,再在上面加水印

    方法: /**  * 图片加水印(适用于png/jpg/gif格式)  *  * @author flynetcn  *  * @param $srcImg  原图片  * @param $water ...

随机推荐

  1. 利用windows server 2012 R2的Hyper-V搭建多个虚拟机的 Dynamics CRM 环境知识点小结

    一.需要掌握网络的知识,域真正的意义,防火墙等其他知识,这些知识我还需要加强,下面是我和同事的结果,不对的地方大家可以指出来,谢谢. 1.安装好的CRM2011环境,必须先打Update Rollup ...

  2. windows server 2008 R2 无法启用"网络发现" 需要启动的服务

    必须打开以下服务: 1.dnscache(简写.fdrespub(简写) 2.SSDP Discovery 3. UPnP Device Host 4. Computer Browser 5.Serv ...

  3. The superclass "javax.servlet.http.HttpServlet" was not found

    在eclipse中,需要通过

  4. python findall() re.S

    官方文档:https://docs.python.org/3.6/library/re.html 教程:http://www.regexlab.com/zh/regref.htm re.findall ...

  5. Android自动化测试中AccessibilityService获取控件信息(1)

    Android自动化测试中AccessibilityService获取控件信息(1) 分类: android自动化测试2014-03-24 15:31 3455人阅读 评论(16) 收藏 举报 and ...

  6. 2018-2019 20165226 Exp5 MSF基础应用

    2018-2019 20165226 Exp5 MSF基础应用 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 主动攻击实践 ms08_067 ms17_010 Task2 针对浏览器 ...

  7. eclipse JDK 下载 and 安装 and 环境配置

    eclipse和JDK软件下载 链接:https://pan.baidu.com/s/1bpRHVIhNtK9_FMVbi34YUQ 密码:y3xr eclipse和JDK这两个软件是配套使用的,适用 ...

  8. binlog之四:mysql中binlog_format模式与配置详解,binlog的日志格式详解

    mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复 ...

  9. [UE4]角色、动画蓝图、动画蒙太奇、动画之间的调用关系

    一.在“角色”中设置要使用的“动画蓝图” 二.在“动画蓝图”中使用“动画”和“混合动画” 三.在“混合动画”中,也可以使用“动画” 四.在角色中使用“动画蒙太奇”

  10. delphi WebBrowser的使用方法详解(三)

    WebBrowser 操作记要 WebBrowser1.GoHome;  //到浏览器默认主页 WebBrowser1.Refresh;  //刷新 WebBrowser1.GoBack;  //后退 ...