再学 GDI+[91]: TGPImage(11) - 转灰度图像
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                再学 GDI+[91]: TGPImage(11) - 转灰度图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            本例效果圖:
代碼文件:
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
                        
                        
                        代碼文件:
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
總結
以上是生活随笔為你收集整理的再学 GDI+[91]: TGPImage(11) - 转灰度图像的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: win32com 读取excel
- 下一篇: visual studio2008 Op
