gdiplus 水印_Delphi程序的应用GDI+制作水印效果图片
利用GDI+可以很方便的制作帶水印效果的圖片,網(wǎng)上介紹這方面的文章也很多,但鮮有Delphi的,本文參照網(wǎng)上文章http://www.codeproject.com/KB/GDI-plus/watermark.aspx介紹的方法,用Delphi 2007制作水印效果圖片,原代碼如下:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Gdiplus, StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure FormPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Phot TGpImage;
PhWidth: Integer;
PhHeight: Integer;
Watermark: TGpImage;
WmWidth: Integer;
WmHeight: Integer;
Bmp: TGpBitmap;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses GdipTypes;
{$R *.dfm}
procedure TMainForm.Button1Click(Sender: TObject);
var
Clsid: TGUID;
Parameters: TEncoderParameters;
Quality: Integer;
begin
// 設(shè)置圖像品質(zhì)編碼參數(shù)
Parameters.Count := 1;
Parameters.Parameter[0].Guid := EncoderQuality;
Parameters.Parameter[0].ValueType := EncoderParameterValueTypeLong;
Parameters.Parameter[0].NumberOfValues := 1;
// 設(shè)置參數(shù)的值:品質(zhì)等級,最高為100,圖像文件大小與品質(zhì)成正比
Quality := 100;
Parameters.Parameter[0].Value := @Quality;
if GetEncoderClsid('image/jpeg', Clsid) then
Bmp.Save('WatermarkPhoto.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);
// 建立新位圖的畫布,并設(shè)置圖像顯示質(zhì)量和文本顯示質(zhì)量
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;
// 設(shè)置透明顏色為水印圖片四角的底色,水印圖顯示為圓角圖片
imageAttr.SetColorKey($ff00ff00, $ff00ff00, ctBitmap);
// 設(shè)置水印圖片不透明度為0.3
imageAttr.SetColorMatrix(ColorMatrix, cfDefault, ctBitmap);
// 在畫布左上角畫水印圖
gp.DrawImage(Watermark, GpRect({PhWidth - WmWidth - }10, 10, WmWidth, WmHeight),
0, 0, WmWidth, WmHeight, utPixel, imageAttr);
// 設(shè)置文本字體和顯示格式
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.
總結(jié)
以上是生活随笔為你收集整理的gdiplus 水印_Delphi程序的应用GDI+制作水印效果图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本月与上月对比数据叫什么_财务人事应会E
- 下一篇: okhttp3测试框架_easy-okh