利用GDI+可以很方便的制作带水印效果的图片,网上介绍这方面的文章也很多,但鲜有Delphi的,本文参照网上文章/uploadfile/201210/12/DC0125775.jpg'', Clsid, @Parameters);
end;
procedure TMainForm.FormCreate(Sender: TObject);
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.3, 0.0),
(0.0, 0.0, 0.0, 0.0, 1.0)
);
WorkingDirectory = ''.Media'';
copyright = ''Copyright © 2008 - Maozefa'';
var
gp: TGpGraphics;
imageAttr: TGpImageAttributes;
strFormat: TGpStringFormat;
font: TGpFont;
x, y: Single;
begin
// 读取原始图片
Photo := TGpImage.Create(WorkingDirectory + ''100_0349.jpg'');
PhWidth := Photo.Width;
PhHeight := Photo.Height;
// 读取水印图片
Watermark := TGpImage.Create(WorkingDirectory + ''Watermark.bmp'');
WmWidth := Watermark.Width;
WmHeight := Watermark.Height;
// 建立一个新的位图,分辨率为72
Bmp := TGpBitmap.Create(PhWidth, PhHeight, pf32bppArgb);
Bmp.SetResolution(72, 72);
// 建立新位图的画布,并设置图像显示质量和文本显示质量
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, 10, WmWidth, WmHeight),
0, 0, WmWidth, WmHeight, utPixel, imageAttr);
// 设置文本字体和显示格式
font := TGpFont.Create(''arial'', 16, [fsBold]);
strFormat := TGpStringFormat.Create;
strFormat.Alignment := saCenter;
// 在画布下方居中显示阴影文本
x := PhWidth / 2;
y := PhHeight - 26;
gp.DrawString(copyright, font, Brushs[$99000000], x + 1, y + 1, strFormat);
gp.DrawString(copyright, font, Brushs[$99ffffff], x, y, strFormat);
font.Free;
strFormat.Free;
imageAttr.Free;
gp.Free;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
Photo.Free;
Watermark.Free;
Bmp.Free;
end;
procedure TMainForm.FormPaint(Sender: TObject);
var
g: TGpGraphics;
begin
g := TGpGraphics.Create(Canvas.Handle);
// 显示原始图片
g.DrawImage(Photo, 0, 0, PhWidth, PhHeight);
// 显示水印原始图片
g.TranslateTransform(0, PhHeight + 5);
g.DrawImage(Watermark, 0, 0, WmWidth, WmHeight);
// 显示带水印和文本的图像
g.TranslateTransform(PhWidth, -(PhHeight + 5));
g.DrawImage(Bmp, 0, 0, PhWidth, PhHeight);
g.Free;
end;
end.