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. 未能正确加载“VSTS for Database Professionals Sql Server Data-tier Application”包。(转)

    今天费了九牛二虎之力,重转好了vs2010之后,打开解决方案,报出下面的错误: ---------------------------Microsoft Visual Studio---------- ...

  2. java 创建子类

    当程序创建子类对象时,系统不仅会为该类中定义的实例变量分配内存,也会为他从父类继承得到的所有实例变量分配内存,即使子类中定义了与父类中同名的实例变量. 如: class Parent { privat ...

  3. Elasticsearch 基础入门

    原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...

  4. php函数的实现

    1.函数     汇编中函数对应的是一组独立的汇编指令,然后通过call指令实现函数的调用.PHP编译的opcode数组,与汇编指令对应. PHP用户自定义函数的实现就是将函数编译为独立的opcode ...

  5. 实验二:C基本数据类型及运算

    2.1  建议使用double型 #include<stdio.h> int main(){ double x,y,z,s,p,a; scanf("%lf%lf%lf" ...

  6. 如何将JQUERY对象转成Javascript对象

    问: <div id="test"></div> $("#test") //由Javascript对象转为Jquery对象: 但是如何转 ...

  7. 配置IIS,以在局域网内访问发布的web站点

    在windows 7或win8 中 配置IIS, 以在局域网内访问自己发布的web 网站或应用程序.主要配置步骤如下: 1. 打开 win7 或 win8 控制面板,选择: 打开或关闭windws 功 ...

  8. python入门-用户输入

    1 input()函数来实现用户输入,程序在等待输入的时候会终止,获取用户的输入后继续 message = input("tell me something,and I will repre ...

  9. 61. oracle给用户解锁

    1.查看用户状态select username,account_status from dba_users where username='test'; 2.解锁: ALTER USER YS_ADM ...

  10. leetcode345

    public class Solution { public string ReverseVowels(string s) { var str = s.ToList(); var Vowels = n ...