[Screenshot Tool] Capture Select Region [Not Whole Screen] lazarus / 39-Graphics ---------------------------------------------------------------------- SniperX · 2016-06-26 09:50 I am able to get the screenshot of full desktop which is drawn out on the form2.canvas Form2 Properties: BorderStyle : bsNone FormStyle : fsSystemStayOnTop Top : 0 Left : 0 WindowState : wsFullScreen Now, i want to draw a rectangle on the form2 and crop out the image from original screenshot based on the Position and width and height of the rectangle. The problem is i do not know how to do it... Also, this is the first time i am working with things related to drawing on form etc so any help is appreciated Thanks ---------------------------------------------------------------------- aradeonas · 2016-06-26 10:18 Welcome, For graphic stuff in Lazarus, we have powerful BGRABitmap. I think you should look at it tutorials and it will be much easy for you and if you got question come back. http://wiki.freepascal.org/BGRABitmap_tutorial http://wiki.freepascal.org/BGRABitmap_tutorial_3 http://wiki.freepascal.org/BGRABitmap_tutorial_5 ---------------------------------------------------------------------- SniperX · 2016-06-26 10:58 Thanks for posting but i don't really think i would get into using BGRABitmap for screenshot purposes but if and when i dive into doing something complicated i would probably end up using it ---------------------------------------------------------------------- aradeonas · 2016-06-26 16:25 Using it is easy and if you want to do graphic stuff do it easily with power. 1-Download and install it. 2-Use this code for simple screen shot. 3-Try to write the code or get help for region select(it will be as easy as other way or easier). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  BGRABitmap,BGRABitmapTypes; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  bmp:TBGRABitmap;begin  bmp:=TBGRABitmap.Create(Monitor.Width,Monitor.Height);  bmp.TakeScreenshotOfPrimaryMonitor;  bmp.Draw(Canvas,0,0);  bmp.Free;end; end. ---------------------------------------------------------------------- SniperX · 2016-06-26 17:56 I already know how to take screenshot of whole screen, i just want to know how to draw a rectangle on a form and crop out the selected region from existing snapshot. ---------------------------------------------------------------------- aradeonas · 2016-06-26 19:34 OK, here is a full test demo. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,  BGRABitmap, BGRABitmapTypes; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Panel1: TPanel;    procedure Button1Click(Sender: TObject);    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);    procedure FormPaint(Sender: TObject);  private    { private declarations }  public    ssbmp: TBGRABitmap;    DS: TPoint;    DR: TRect;    MD: boolean;    procedure CalculateDR(X, Y: integer);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  ssbmp.Free;  ssbmp := TBGRABitmap.Create(Monitor.Width, Monitor.Height);  ssbmp.TakeScreenshotOfPrimaryMonitor;  Invalidate;end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin    MD := True;   with DR do  begin  Left := X;    Top := Y;    Right := Left;    Bottom := Top;  end;  DS.X := DR.Left;  DS.Y := DR.Top;end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);begin    if not MD then    Exit;  CalculateDR(X, Y);  Invalidate;end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin    MD := False;  Invalidate;end; procedure TForm1.FormPaint(Sender: TObject);var  bmp: TBGRABitmap;  regionbmp:TBGRABitmap;begin  if not Assigned(ssbmp) then    Exit;  bmp := TBGRABitmap.Create(Width, Height);  bmp.PutImage(0, 0, ssbmp, dmSet);  if MD then  begin    bmp.Rectangle(DR.Left, DR.Top, DR.Right, DR.Bottom, BGRA(255, 0, 0, 200), dmDrawWithTransparency);    regionbmp:=bmp.GetPart(DR) as TBGRABitmap;    if Assigned(regionbmp) then    regionbmp.Draw(Panel1.Canvas,0,0);    //Do what you want here, if you want just save it after selection is done do this stuff in mouseup    regionbmp.Free;  end;  bmp.Draw(Canvas, 0, 0);  bmp.Free;end; procedure TForm1.CalculateDR(X, Y: integer);begin  if x >= DS.x then  begin    DR.Left := DS.x;    DR.Right := x;  end  else  begin    DR.Right := DS.x;    DR.Left := x;  end;  if y >= DS.y then  begin    DR.Top := DS.y;    DR.Bottom := y;  end  else  begin    DR.Bottom := DS.y;    DR.Top := y;  end;end; end. Test the attachment. ---------------------------------------------------------------------- molly · 2016-06-26 19:43 Quote from: SniperX on June 26, 2016, 07:56:29 pm i just want to know how to draw a rectangle on a form Code: [Select]Self.Canvas.DrawFocusRect(FSelectionRect); Where self is the form and FSelectionRect the rectangle that was selected Quote... and crop out the selected region from existing snapshot. Code: [Select]var   Bitmap : TBitmap;   W,H    : Integer; begin     Bitmap := TBitmap.Create;     W := FSelectionRect.Right  - FSelectionRect.Left;     H := FSelectionRect.Bottom - FSelectionRect.Top;     Bitmap.SetSize(W, H);     Bitmap.Canvas.CopyRect(Rect(0,0,W,H), Self.Canvas, FSelectionRect);     Bitmap.SaveToFile('MyCroppedScreenShot.bmp');     Bitmap.Free; end; Where self is the form and FSelectionRect the rectangle that was selected edit: whoops and sorry. I completely forgot to add the project as attachment. Hmz, never mind as howardpc provided an excellent example of the whole thing. I added mine just in case interested: My selection procedure is a bit different. ---------------------------------------------------------------------- howardpc · 2016-06-26 22:09 Here's another  example using  TBitmap rather than TBGRABitmap. ---------------------------------------------------------------------- SniperX · 2016-06-26 23:26 @aradeonas @ molly @howardpc  Thanks for all the effort you all put in, im really really thankfull to all of you i just need to ask two things from howardpc though 1) Is it possible to highlight the selection rectangle with red color while the selction process is in action and before the mouseup event fires ? 2) Can we save the selected image without the colored highlight to desktop ? Thanks for everyones help Edit: 1) Instead of DrawFocusRect i used Rectangle() 2) Commented out the use of Frame() method which excludes the selection ink before saving the image