代码文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)CheckBox1: TCheckBox;procedure FormCreate(Sender: TObject);procedure FormPaint(Sender: TObject);procedure CheckBox1Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}uses GDIPOBJ, GDIPAPI;const{一般模式}ColorMatrix1: 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));{灰度模式}ColorMatrix2: TColorMatrix = ((0.3, 0.3, 0.3, 0.0, 0.0),(0.59, 0.59, 0.59, 0.0, 0.0),(0.11, 0.11, 0.11, 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); beginCheckBox1.Caption := '转为灰度'; end;procedure TForm1.FormPaint(Sender: TObject); varg: TGPGraphics;img: TGPImage;ImageAttributes: TGPImageAttributes; beging := TGPGraphics.Create(Canvas.Handle);img := TGPImage.Create('c:\temp\test.png');ImageAttributes := TGPImageAttributes.Create;if CheckBox1.Checked thenImageAttributes.SetColorMatrix(ColorMatrix2)elseImageAttributes.SetColorMatrix(ColorMatrix1);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.CheckBox1Click(Sender: TObject); beginRepaint; end;end.窗体文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 216ClientWidth = 269Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePosition = poDesktopCenterOnCreate = FormCreateOnPaint = FormPaintPixelsPerInch = 96TextHeight = 13object CheckBox1: TCheckBoxLeft = 183Top = 191Width = 79Height = 17Caption = 'CheckBox1'TabOrder = 0OnClick = CheckBox1Clickend end