首页 > 代码库 > 如何获取图片中第一个像素的颜色值?

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

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  

  

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