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. JSP include 指令

    1. 创建test5.jsp test5_1.jsp test5_2.jsp test5_1.jsp <%@ page import="java.util.*" %> ...

  2. 关于innodb_thread_concurrency参数 并发控制

    http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_thread_concurrency Comma ...

  3. 这些 .Net and Core 相关的开源项目,你都知道吗?(持续更新中...)

    最近更新时间2017-12-28 序列化 Json.NET http://json.codeplex.com/Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Ne ...

  4. Linux内核编译:很少有人提及的一些内容

    1. 你可以使用O=参数将编译结果放到其他位置(非源代码目录),例如:make O=~/build ... 这样做的好处是你的源代码目录不会受到任何改变:你甚至可以在不同的体系结构间共享源代码. 注意 ...

  5. Hadoop高级培训课程大纲-管理员版

    一.课程概述 本次培训课程主要面向大数据系统管理人员和开发设计人员,基于开源社区大数据应用最活跃的Hadoop和HBase技术框架.围绕分布式文件存储(HDFS).分布式并行计算(Map/Recue) ...

  6. DOM操作之属性和样式操作

    在DOM操作,除了前面的节点操作以外,常常被用到的操作还有属性操作和节点操作,下面,主要来总结一下jQuery中的属性操作方法和样式操作方法. 在开始操作前,我们需要先在html中添加如下代码,后面所 ...

  7. 转载-WebLogic使用总结

    WebLogic使用总结(七)——WebLogic部署Web应用并绑定域名 孤傲苍狼 2015-01-13 15:19 阅读:2472 评论:1     WebLogic使用总结(六)——WebLog ...

  8. 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题

    cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...

  9. Nginx的ip_hash指令

    ip_hash 语法:ip_hash 默认值:none 使用环境:upstream 当对后端的多台动态应用服务器做负载均衡时,ip_hash指令能够将某个客户端IP的请求通过哈希算法定位到同一台后端服 ...

  10. LightGBM优势总结

    效率和内存上的提升 1) 在训练决策树计算切分点的增益时,xgboost采用预排序,即需要对每个样本的切分位置都要计算一遍,所以时间复杂度是O(#data). 而LightGBM则是将样本离散化为直方 ...