Recent

Author Topic: [Screenshot Tool] Capture Select Region [Not Whole Screen]  (Read 9772 times)

SniperX

  • New Member
  • *
  • Posts: 13
[Screenshot Tool] Capture Select Region [Not Whole Screen]
« on: June 26, 2016, 11:50:36 am »
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 :)



« Last Edit: June 26, 2016, 08:04:02 pm by SniperX »

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: [Screenshot Tool] Select Region
« Reply #1 on: June 26, 2016, 12:18:10 pm »
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

  • New Member
  • *
  • Posts: 13
Re: [Screenshot Tool] Select Region
« Reply #2 on: June 26, 2016, 12:58:19 pm »
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

  • Hero Member
  • *****
  • Posts: 824
Re: [Screenshot Tool] Select Region
« Reply #3 on: June 26, 2016, 06:25:04 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   BGRABitmap,BGRABitmapTypes;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.   bmp:TBGRABitmap;
  36. begin
  37.   bmp:=TBGRABitmap.Create(Monitor.Width,Monitor.Height);
  38.   bmp.TakeScreenshotOfPrimaryMonitor;
  39.   bmp.Draw(Canvas,0,0);
  40.   bmp.Free;
  41. end;
  42.  
  43. end.
  44.  

SniperX

  • New Member
  • *
  • Posts: 13
Re: [Screenshot Tool] Select Region
« Reply #4 on: June 26, 2016, 07:56:29 pm »
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

  • Hero Member
  • *****
  • Posts: 824
Re: [Screenshot Tool] Capture Select Region [Not Whole Screen]
« Reply #5 on: June 26, 2016, 09:34:56 pm »
OK, here is a full test demo.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.   BGRABitmap, BGRABitmapTypes;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Panel1: TPanel;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  20.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  21.     procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  22.     procedure FormPaint(Sender: TObject);
  23.   private
  24.     { private declarations }
  25.   public
  26.     ssbmp: TBGRABitmap;
  27.     DS: TPoint;
  28.     DR: TRect;
  29.     MD: boolean;
  30.     procedure CalculateDR(X, Y: integer);
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. begin
  44.   ssbmp.Free;
  45.   ssbmp := TBGRABitmap.Create(Monitor.Width, Monitor.Height);
  46.   ssbmp.TakeScreenshotOfPrimaryMonitor;
  47.   Invalidate;
  48. end;
  49.  
  50. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  51. begin
  52.     MD := True;
  53.  
  54.   with DR do
  55.   begin
  56.     Left := X;
  57.     Top := Y;
  58.     Right := Left;
  59.     Bottom := Top;
  60.   end;
  61.   DS.X := DR.Left;
  62.   DS.Y := DR.Top;
  63. end;
  64.  
  65. procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  66. begin
  67.     if not MD then
  68.     Exit;
  69.   CalculateDR(X, Y);
  70.   Invalidate;
  71. end;
  72.  
  73. procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  74. begin
  75.     MD := False;
  76.   Invalidate;
  77. end;
  78.  
  79. procedure TForm1.FormPaint(Sender: TObject);
  80. var
  81.   bmp: TBGRABitmap;
  82.   regionbmp:TBGRABitmap;
  83. begin
  84.   if not Assigned(ssbmp) then
  85.     Exit;
  86.   bmp := TBGRABitmap.Create(Width, Height);
  87.   bmp.PutImage(0, 0, ssbmp, dmSet);
  88.   if MD then
  89.   begin
  90.     bmp.Rectangle(DR.Left, DR.Top, DR.Right, DR.Bottom, BGRA(255, 0, 0, 200), dmDrawWithTransparency);
  91.     regionbmp:=bmp.GetPart(DR) as TBGRABitmap;
  92.     if Assigned(regionbmp) then
  93.     regionbmp.Draw(Panel1.Canvas,0,0);
  94.     //Do what you want here, if you want just save it after selection is done do this stuff in mouseup
  95.     regionbmp.Free;
  96.   end;
  97.   bmp.Draw(Canvas, 0, 0);
  98.   bmp.Free;
  99. end;
  100.  
  101. procedure TForm1.CalculateDR(X, Y: integer);
  102. begin
  103.   if x >= DS.x then
  104.   begin
  105.     DR.Left := DS.x;
  106.     DR.Right := x;
  107.   end
  108.   else
  109.   begin
  110.     DR.Right := DS.x;
  111.     DR.Left := x;
  112.   end;
  113.   if y >= DS.y then
  114.   begin
  115.     DR.Top := DS.y;
  116.     DR.Bottom := y;
  117.   end
  118.   else
  119.   begin
  120.     DR.Bottom := DS.y;
  121.     DR.Top := y;
  122.   end;
  123. end;
  124.  
  125. end.
  126.  
Test the attachment.
« Last Edit: June 26, 2016, 09:38:54 pm by aradeonas »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [Screenshot Tool] Select Region
« Reply #6 on: June 26, 2016, 09:43:33 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.
« Last Edit: June 27, 2016, 12:23:56 am by molly »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Screenshot Tool] Capture Select Region [Not Whole Screen]
« Reply #7 on: June 27, 2016, 12:09:02 am »
Here's another  example using  TBitmap rather than TBGRABitmap.

SniperX

  • New Member
  • *
  • Posts: 13
Re: [Screenshot Tool] Capture Select Region [Not Whole Screen]
« Reply #8 on: June 27, 2016, 01:26:00 am »
@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 :)
« Last Edit: June 27, 2016, 02:06:46 am by SniperX »

 

TinyPortal © 2005-2018