作者:兔女郎不会吧 | 来源:互联网 | 2023-08-22 17:42
unitUWaterMark;interfaceuses{$IFNDEFDELPHIXE2ANDUP}windows,SysUtils,classes,graphics,Gdipl
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;