如何获取图片中第一个像素的颜色值?

unit Unit1;  
  
interface  
  
uses  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  Dialogs, StdCtrls;  
  
type  
  TForm1 = class(TForm)  
    Button1: TButton;  
    Button2: TButton;  
    procedure Button1Click(Sender: TObject);  
    procedure Button2Click(Sender: TObject);  
  end;  
  
var  
  Form1: TForm1;  
  
implementation  
  
{$R *.dfm}  
  
const BmpPath = 'C:/Temp/Test.bmp'; {测试用图片}  
  
{使用 Canvas.Pixels 属性}  
procedure TForm1.Button1Click(Sender: TObject);  
var  
  bit: TBitmap;  
  clr: TColor;  
begin  
  bit := TBitmap.Create;  
  bit.LoadFromFile(BmpPath);      {载入图片}  
  clr := bit.Canvas.Pixels[0, 0]; {获取第一像素的颜色值}  
  ShowMessageFmt('$%.6x', [clr]); {显示为十六进制字符串看看}  
  //Self.Color := clr;            {可用窗体呈现一下}  
  bit.Free;  
end;  
  
{使用 API 函数 GetPixel}  
procedure TForm1.Button2Click(Sender: TObject);  
var  
  bit: TBitmap;  
  clr: TColor;  
begin  
  bit := TBitmap.Create;  
  bit.LoadFromFile(BmpPath);               {载入图片}  
  clr := GetPixel(bit.Canvas.Handle, 0, 0);{获取第一像素的颜色值}  
  ShowMessageFmt('$%.6x', [clr]);          {显示为十六进制字符串看看}  
  //Self.Color := clr;                     {可用窗体呈现一下}  
  bit.Free;  
end;  
  
end  

  

posted @ 2014-10-18 13:52  孤舟残月浅笑嫣然  阅读(520)  评论(0编辑  收藏  举报