Recent

Author Topic: Getting Color of certain pixel on screen  (Read 2873 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Getting Color of certain pixel on screen
« on: July 22, 2021, 07:06:57 pm »
Hi,

I want
a) to determine the current background color value of a form
b) to determine the color (RGB, TColor) of a certain point on the screen.

Can you help?

Thx
Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Getting Color of certain pixel on screen
« Reply #1 on: July 22, 2021, 09:17:12 pm »
Hi!

Code: Pascal  [Select][+][-]
  1. var
  2.   ScreenDC: HDC;
  3.   RGB, BGcol : TColor;
  4.  
  5. begin
  6.     // a)
  7.    BGcol := Form1.Color;
  8.  
  9.    // b) get color of pixel  10/20
  10.   ScreenDC := GetDC(0);
  11.   RGB := GetPixel(ScreenDC 10, 20);
  12.  
  13. end;

Winni

440bx

  • Hero Member
  • *****
  • Posts: 4037
Re: Getting Color of certain pixel on screen
« Reply #2 on: July 22, 2021, 09:27:32 pm »
I want
a) to determine the current background color value of a form
b) to determine the color (RGB, TColor) of a certain point on the screen.
I can only tell you how to do it using the Windows API.  For a multiplatform answer, someone else will have to provide it.

for (a) use GetClassLongPtr using GCL_HBRBACKGROUND

for (b) use GetPixel

both of those functions are documented by MS.  Use Google to get to the detailed documentation.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Getting Color of certain pixel on screen
« Reply #3 on: July 22, 2021, 10:48:49 pm »
The following code is taken from the mbColorLib package and is cross-platform:

Code: Pascal  [Select][+][-]
  1. function ReadScreenColor(const X, Y: integer): TColor;
  2. var
  3.   ScreenDC: HDC;
  4.   SaveBitmap: TBitmap;
  5. begin
  6.   SaveBitmap := TBitmap.Create;
  7.   try
  8.     SaveBitmap.SetSize(Screen.Width, Screen.Height);
  9.     ScreenDC := GetDC(0);
  10.     try
  11.       SaveBitmap.LoadFromDevice(ScreenDC);
  12.     finally
  13.       ReleaseDC(0, ScreenDC);
  14.     end;
  15.     Result := SaveBitmap.Canvas.Pixels[X, Y];
  16.   finally
  17.     SaveBitmap.Free;
  18.   end;
  19. end;

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Getting Color of certain pixel on screen
« Reply #4 on: July 22, 2021, 10:52:02 pm »
Hi!

Yes, that is the long version for a HowTo-Book.

I showed the short version above.

Winni

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Getting Color of certain pixel on screen
« Reply #5 on: July 22, 2021, 10:55:48 pm »
Thank you all!

Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Getting Color of certain pixel on screen
« Reply #6 on: July 22, 2021, 10:57:46 pm »
I showed the short version above.
To my knowledge GetPixel is a function in the Windows unit. If it were cross-platform, it would be found in LCLIntf.

 

TinyPortal © 2005-2018