本例效果图:
代码文件:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ListBox1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses GDIPOBJ, GDIPAPI; var n: Single = 0.75; {缩放倍数} procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Align := alRight; with ListBox1.Items do begin add('InterpolationModeInvalid '); add('InterpolationModeDefault '); add('InterpolationModeLowQuality '); add('InterpolationModeHighQuality '); add('InterpolationModeBilinear '); add('InterpolationModeBicubic '); add('InterpolationModeNearestNeighbor '); add('InterpolationModeHighQualityBilinear'); add('InterpolationModeHighQualityBicubic '); end; ListBox1.ItemIndex := 1; end; procedure TForm1.FormPaint(Sender: TObject); var g: TGPGraphics; img: TGPImage; rt: TGPRectF; begin g := TGPGraphics.Create(Self.Canvas.Handle); img := TGPImage.Create('C:\temp\test.png'); g.DrawImage(img, 4, 4, img.GetWidth, img.GetHeight); rt := MakeRect(4+img.GetWidth+4, 4, img.GetWidth * n, img.GetHeight * n); g.SetInterpolationMode(ListBox1.ItemIndex - 1); g.DrawImage(img, rt, 0, 0, img.GetWidth, img.GetHeight, UnitPixel); img.Free; g.Free; end; procedure TForm1.ListBox1Click(Sender: TObject); begin Repaint; end; end.窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 213 ClientWidth = 550 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesktopCenter OnCreate = FormCreate OnPaint = FormPaint PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 357 Top = 32 Width = 185 Height = 97 ItemHeight = 13 TabOrder = 0 OnClick = ListBox1Click end end缩放时的算法模式(TInterpolationMode 枚举)列表:
InterpolationModeInvalid = -1; {等效于 QualityMode 枚举的 Invalid 元素. } InterpolationModeDefault = 0; {指定默认模式. } InterpolationModeLowQuality = 1; {指定低质量插值法. } InterpolationModeHighQuality = 2; {指定高质量插值法. } InterpolationModeBilinear = 3; {指定双线性插值法. 不进行预筛选. 将图像收缩为原始 大小的 50% 以下时,此模式不适用. } InterpolationModeBicubic = 4; {指定双三次插值法. 不进行预筛选. 将图像收缩为原始 大小的 25% 以下时,此模式不适用. } InterpolationModeNearestNeighbor = 5; {指定最临近插值法. } InterpolationModeHighQualityBilinear = 6; {指定高质量的双线性插值法. 执行预筛选以确保高质量 的收缩. } InterpolationModeHighQualityBicubic = 7; {指定高质量的双三次插值法. 执行预筛选以确保高质量 的收缩. 此模式可产生质量最高的转换图像. }