Recent

Author Topic: [Solved] Screen block copy  (Read 1296 times)

Ruptor

  • Full Member
  • ***
  • Posts: 184
[Solved] Screen block copy
« on: April 17, 2019, 12:18:56 am »
This has probably been done a million times before so if someone can point me to an example I would appreciate it. I want to copy a small part of the screen to analyse so I thought it would be good to see the copied block in a picture on my form but the method I have used gives rubbish. It doesn't match any part of the screen let alone the bit I want although some of the colours seem to match it just looks scrambled up. The size of the picture matches the block I mark on the screen with top left and bottom right mouse positions but the content doesn't. :(
Here is the code that does the copying and placing in a picture that I cobbled together by looking at the Wiki and I could understand or maybe not.
Code: Pascal  [Select][+][-]
  1.         masbmp := TBitmap.Create;
  2.         masbmp.Height := ymbot-ymtop;
  3.         masbmp.Width := xmmax;
  4.         mastp.Y:=tp.Y;
  5.         mastp.X:=tp.X;
  6.         for py:=ymtop to ymbot do
  7.         begin
  8.           for px:=0 to xmmax-1 do
  9.           begin
  10.             masbmp.Canvas.Pixels[px,py]:=ScrBitmap.Canvas.Pixels[mastp.X,mastp.Y];
  11.             mastp.X:=mastp.X+1;
  12.           end;
  13.           mastp.Y:=mastp.Y+1;
  14.         end;
  15.         with Image1.Picture.Bitmap do begin
  16.           SetSize(xmmax,ymmax);
  17.         end;
  18.         Image1.Canvas.Draw(0,0,masbmp);
  19.  
« Last Edit: April 19, 2019, 10:54:27 am by Ruptor »

Handoko

  • Hero Member
  • *****
  • Posts: 4153
  • My goal: build my own game engine using Lazarus
Re: Screen block copy
« Reply #1 on: April 17, 2019, 04:19:05 am »
Not exactly what you want. But I think you may be interested to know how to analyze image per pixel:

https://forum.lazarus.freepascal.org/index.php/topic,37242.msg252828.html#msg252828

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 649
Re: Screen block copy
« Reply #2 on: April 17, 2019, 02:11:24 pm »
Not sure, if there is some cross-platform way to do it, but on Windows it's pretty simple:
Code: Pascal  [Select][+][-]
  1. uses Windows;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4.   var DesktopCanvas:TCanvas;
  5.   MyRect, DestRect:TRect;
  6. begin
  7.   MyRect := TRect.Create(100, 100, 150, 150);
  8.   DestRect := MyRect;
  9.   DestRect.Offset(-DestRect.Left, -DestRect.Top);
  10.   DesktopCanvas := TCanvas.Create;
  11.   DesktopCanvas.Handle := GetDC(0);
  12.   Image1.Picture.Bitmap.Width := DestRect.Width;
  13.   Image1.Picture.Bitmap.Height := DestRect.Height;
  14.   Image1.Picture.Bitmap.Canvas.CopyRect(
  15.     DestRect,
  16.     DesktopCanvas,
  17.     MyRect
  18.   );
  19.   DesktopCanvas.Free;
  20. end;  
  21.  
DynamicData 3.0 is released!
4.0 version is in development! Another complete overhaul! Things are made more simple and, of course, faster!
Aiming at Lazarus support, when closures will be implemented.

Ruptor

  • Full Member
  • ***
  • Posts: 184
Re: Screen block copy
« Reply #3 on: April 17, 2019, 02:46:59 pm »
Thanks Guys. I had viewed the colour grabbing posts when first starting to try and understand graphics stuff. I shall try and adapt the rectangle copy and see if that works for me. Perhaps it will show what fault I have created in my code. :o

Ruptor

  • Full Member
  • ***
  • Posts: 184
Re: Screen block copy
« Reply #4 on: April 19, 2019, 10:54:00 am »
Having collected the top left ,tp, and bottom right ,bp, screen coordinates of the rectangle I want to copy this code works on Linux and puts the screen rectangle in the picture rectangle on my form. :) Thanks for the help.
Code: Pascal  [Select][+][-]
  1.         DesktopCanvas := TCanvas.Create;
  2.         DesktopCanvas.Handle := GetDC(0);
  3.         screct:= TRect.Create(tp,bp);
  4.         masrect:= TRect.Create(0,0,xmmax-1,ymmax-1);
  5.         with Image1.Picture.Bitmap do begin
  6.           SetSize(xmmax,ymmax);
  7.         end;
  8.         Image1.Picture.Bitmap.Canvas.CopyRect(masrect,DesktopCanvas,screct);
  9.         DesktopCanvas.Free;
  10.  

 

TinyPortal © 2005-2018