Forum > BGRABitmap and LazPaint

Using TBgraBitmap for screenshot - only one image results

(1/2) > >>

BosseB:
I have created a screen capture utility, which I use a lot but it has a very annoying behaviour:
- I adjust the rect to capture a certain area
- I make the screenshot and put it into the clipboard
- I paste it into my image editor
So far all works fine.
- Next I scroll the window I am capturing images from so that the next section I want to clip is inside the rect.
- Again I take a screenshot
- I paste it into the image editor and see the previous capture duplicated!

The only way I have found to get around this is to move and resize the capture rect and then move it back and re-adjust to the same size as before.
Then a capture works to get the correct image!

What am I doing wrong here?

My capture function looks like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---private      FMyCapture: TBgraBitmap;... procedure TfrmMain.miCopyImageClick(Sender : TObject);begin  Sleep(300); //To let the pop-up menu disappear  CopyScreenRect(0);end; procedure TfrmMain.CopyScreenRect(arg: PtrInt);var  Clip: TRect;  X, Y, W, H: integer;begin  try    Clip := Bounds(frmMain.Left, frmMain.Top, frmMain.Width, frmMain.Height);    X := frmMain.Left;    Y := frmMain.Top;    W := frmMain.Width;    H := frmMain.Height;    frmMain.Visible := false; //Hide the form defining Clip    FMyCapture.TakeScreenShot(Clip);    frmMain.Visible := true;    frmMain.Left := X;    frmMain.Top :=  Y;    frmMain.Width := W;    frmMain.Height := H;    Clipboard.Assign(FMyCapture.Bitmap);  except    on E: exception do      Clipboard.AsText := E.Message;  end;end; 

loaded:
It would be useful to have a look at the demo in the link.
https://forum.lazarus.freepascal.org/index.php/topic,57244.msg425599.html#msg425599

winni:
Hi!

Use this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...var bmp : TBGRABitmap;    R : TRect;begin  R := Rect (100,100,400,400);  bmp.TakeScreenshot(R);        ....end;
Winni

BosseB:

--- Quote from: winni on January 03, 2022, 12:39:36 pm ---Use this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...var bmp : TBGRABitmap;    R : TRect;begin  R := Rect (100,100,400,400);  bmp.TakeScreenshot(R);        ....end;Winni

--- End quote ---
But that is what I am doing, except I create the container using this in FormCreate:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FMyCapture := TBgraBitmap.Create;You are not showing if you have created it...
Concerning the rect coordinates, you are setting a fixed size whereas I am using the form as a template:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  Clip: TRect;  X, Y, W, H: integer;begin  try    Clip := Bounds(frmMain.Left, frmMain.Top, frmMain.Width, frmMain.Height);
Note that my problem is not how to take the screenshot but the fact that if the area contained by the rect changes content between two screenshots then the old already captured screenshot is returned instead of a new one.
But if I move the form and change its size and then go back to the old position and size then the new clip will be with new content...

UPDATE:
As a test I modified my code to use a local TBGRABitmap variable inside the CopyScreenRect function where it is created-used-destroyed and then it works fine.

But my program has other functions as well, for instance to save into a disk file and these use the global object I have shown here. So when the object is used that way the problem appears.

BosseB:
So if I use this code for capturing then it works OK:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfrmMain.CopyScreenRect(arg: PtrInt);var  Clip: TRect;  X, Y, W, H: integer;begin  if Assigned(FMyCapture) then FMyCapture.Free; //To clear any remaining image  FMyCapture := TBgraBitmap.Create;  try    X := frmMain.Left;    Y := frmMain.Top;    W := frmMain.Width;    H := frmMain.Height;    Clip := Bounds(X, Y, W, H);    frmMain.Visible := false;    FMyCapture.TakeScreenShot(Clip);    frmMain.Visible := true;    frmMain.Left := X;    frmMain.Top :=  Y;    frmMain.Width := W;    frmMain.Height := H;    Clipboard.Assign(FMyCapture.Bitmap);  except    on E: exception do      Clipboard.AsText := E.Message;  end;end; 
I do not understand why it does not work in the original version but doing this create/free inside the function is a workaround in any case.
And my other usage of FMyCapture was a mis-read of my code. Those functions actually call this before doing their thing...

Navigation

[0] Message Index

[#] Next page

Go to full version