再学GDI+[95]: TGPImage(15)

我通过做此例同时证实了 GDI+ 可以直接显示 png、gif、tif 格式的透明图片.

//红色与绿色绕蓝色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
 (Cos(r),  Sin(r), 0.0, 0.0, 0.0),
 (-Sin(r), Cos(r), 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, 1.0));
//绿色与蓝色绕红色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
 (0.0, 0.0,   0.0,   0.0, 0.0),
 (0.0, Cos(r), Sin(r), 0.0, 0.0),
 (0.0, -Sin(r), Cos(r), 0.0, 0.0),
 (0.0, 0.0,   0.0,   1.0, 0.0),
 (0.0, 0.0,   0.0,   0.0, 1.0));
//红色与蓝色绕绿色旋转(r 是弧度)
ColorMatrix: TColorMatrix = (
 (Cos(r),  0.0, Sin(r), 0.0, 0.0),
 (-Sin(r), 0.0, Cos(r), 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, 1.0));
本例效果图:

代码文件:unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls, ComCtrls, ExtCtrls;
type
 TForm1 = class(TForm)
  TrackBar1: TTrackBar;
  RadioGroup1: TRadioGroup;
  procedure FormCreate(Sender: TObject);
  procedure FormPaint(Sender: TObject);
  procedure CheckBox1Click(Sender: TObject);
  procedure TrackBar1Change(Sender: TObject);
  procedure RadioGroup1Click(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses GDIPOBJ, GDIPAPI;
const
 Matrix: 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, 1.0, 0.0),
  (0.0, 0.0, 0.0, 0.0, 1.0));
procedure TForm1.FormCreate(Sender: TObject);
begin
 TrackBar1.Height := 23;
 TrackBar1.ShowSelRange := False;
 TrackBar1.Min := -180;
 TrackBar1.Max := 180;
 TrackBar1.Position := 0;
 with RadioGroup1.Items do
 begin
  Add('红色与绿色绕蓝色旋转');
  Add('红色与蓝色绕绿色旋转');
  Add('绿色与蓝色绕红色旋转');
 end;
 RadioGroup1.ItemIndex := 0;
 DoubleBuffered := True;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
 g: TGPGraphics;
 img: TGPImage;
 ImageAttributes: TGPImageAttributes;
 r: Single;
 ColorMatrix: TColorMatrix;
begin
 g := TGPGraphics.Create(Canvas.Handle);
 img := TGPImage.Create('c:\temp\test.png');
 ImageAttributes := TGPImageAttributes.Create;
 r := TrackBar1.Position * pi / 180; {根据角度算出弧度}
 ColorMatrix := Matrix;       {回复默认值}
 case RadioGroup1.ItemIndex of
  0: begin {红色与绿色绕蓝色旋转}
   ColorMatrix[0,0] := Cos(r);
   ColorMatrix[0,1] := Sin(r);
   ColorMatrix[1,0] := -Sin(r);
   ColorMatrix[1,1] := Cos(r);
  end;
  1: begin {红色与蓝色绕绿色旋转}
   ColorMatrix[0,0] := Cos(r);
   ColorMatrix[0,2] := Sin(r);
   ColorMatrix[2,0] := -Sin(r);
   ColorMatrix[2,2] := Cos(r);
  end;
  2: begin {绿色与蓝色绕红色旋转}
   ColorMatrix[1,1] := Cos(r);
   ColorMatrix[1,2] := Sin(r);
   ColorMatrix[2,1] := -Sin(r);
   ColorMatrix[2,2] := Cos(r);
  end;
 end;
 ImageAttributes.SetColorMatrix(ColorMatrix);
 g.DrawImage(img,
       MakeRect(4, 4, img.GetWidth, img.GetHeight),
       0,
       0,
       img.GetWidth,
       img.GetHeight,
       UnitPixel,
       ImageAttributes
       );
 ImageAttributes.Free;
 img.Free;
 g.Free;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
 Repaint;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
 Repaint;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
 Repaint;
end;
end.

窗体文件:object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 290
 ClientWidth = 208
 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 TrackBar1: TTrackBar
  Left = 3
  Top = 170
  Width = 202
  Height = 22
  TabOrder = 0
  OnChange = TrackBar1Change
 end
 object RadioGroup1: TRadioGroup
  Left = 8
  Top = 198
  Width = 190
  Height = 83
  Caption = 'RadioGroup1'
  TabOrder = 1
  OnClick = RadioGroup1Click
 end
end

时间: 2024-08-31 16:05:23

再学GDI+[95]: TGPImage(15)的相关文章

再学GDI+[99]: TGPImage(19)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) OpenDialog1: TOpenDialog; Button1: TButton; Button2: TButton; procedure FormCrea

再学GDI+[97]: TGPImage(17)

获取GDI+所支持的可编码.可解码的图像格式 其实这和 TGPImage 是没有关系的. 本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TO

再学GDI+[81]: TGPImage(1)

已知 GDI+ 可以支持的图像格式: BMP.JPEG.GIF.TIFF.PNG.ICO.WMF.EMF TGPGraphics.DrawImage 函数有太多重载了, 一起列在这吧:function DrawImage(image: TGPImage; const point: TGPPointF): TStatus; overload; function DrawImage(image: TGPImage; x, y: Single): TStatus; overload; function

再学GDI+[103]: TGPImage(23)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; OpenDialog1: TOpenDialog; procedure FormCreate(Sende

再学GDI+[100]: TGPImage(20)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TeCanvas; type TForm1 = class(TForm) ButtonColor1: TButtonColor; Button1: TButton; procedure FormCreate(Sender: TObje

再学GDI+[94]: TGPImage(14)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) TrackBar1: TTrackBar; TrackBar2: TTrackBar; TrackBar3: TTrackBar; Label1: TLabel

再学GDI+[93]: TGPImage(13)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) TrackBar1: TTrackBar; TrackBar2: TTrackBar; TrackBar3: TTrackBar; TrackBar4: TTr

再学GDI+[92]: TGPImage(12)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) TrackBar1: TTrackBar; procedure FormCreate(Sender: TObject); procedure FormPaint

再学GDI+[91]: TGPImage(11)

本例效果图: 代码文件:unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) CheckBox1: TCheckBox; procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: T