* * *

Author Topic: grabbing the colour of a pixel from the coordinates of the pixel in a bitmap or  (Read 203 times)

John_Nicol

  • Newbie
  • Posts: 2
I want to be able to provide the pixel coordinates in a picture and obtain a numerical or string definition of the colour represented by that pixel.  Can someone provide a clear, simple code which will run in Lazrus Free Pascal? Thank you.
John Nicol

Handoko

  • Hero Member
  • *****
  • Posts: 1177
  • My goal: build my own game engine using Lazarus
Hello John_Nicol,
Welcome to the forum.

I wrote a simple example, it should be easy to understand. You can download the test.zip. Next time, please do not do double post. :D

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, Dialogs, StdCtrls, ExtCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Image1: TImage;
  17.     Label1: TLabel;
  18.     OpenDialog1: TOpenDialog;
  19.     Shape1: TShape;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
  22.       );
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. var
  40.   AJpg:  TJPEGImage;
  41. begin
  42.   if not(OpenDialog1.Execute) then Exit;
  43.   AJpg := TJpegImage.Create;
  44.   AJpg.LoadFromFile(OpenDialog1.FileName);
  45.   Image1.Picture.Bitmap.Assign(AJpg);
  46.   AJpg.Free;
  47.   Image1.Enabled := True;
  48. end;
  49.  
  50. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  51.   Y: Integer);
  52. var
  53.   ScanData: PRGBQuad;
  54.   ValR, ValG, ValB: Byte;
  55. begin
  56.   // Point to the pixel location
  57.   ScanData := Image1.Picture.Bitmap.ScanLine[Y];
  58.   Inc(ScanData, X);
  59.   // Get RGB value of the pixel
  60.   ValR := ScanData^.rgbRed;
  61.   ValG := ScanData^.rgbGreen;
  62.   ValB := ScanData^.rgbBlue;
  63.   // Show information of the pixel
  64.   Shape1.Brush.Color := RGBToColor(ValR, ValG, ValB);
  65.   Label1.Caption := 'x'+IntToStr(X)+':y'+IntToStr(Y)+' = r'+
  66.     IntToStr(ValR)+', g'+IntToStr(ValG)+', b'+IntToStr(ValB);
  67. end;
  68.  
  69. end.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 607
    • Burdjia
Please don't duplicate your threads. It doesn't help to have more ansers (as you see) but fills the forums with shitty stuff.

Thanks.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus