Lazarus

Programming => Widgetset => Cocoa => Topic started by: MISV on August 04, 2019, 04:53:15 pm

Title: Get screnshot of of control/contents (here webview)
Post by: MISV on August 04, 2019, 04:53:15 pm
I have various solutions in Objective C here

https://stackoverflow.com/questions/3458085/take-snapshot-screenshot-of-uiwebview
https://stackoverflow.com/questions/11948241/cocoa-how-to-render-view-to-image
https://stackoverflow.com/questions/34490356/converting-webview-into-a-bitmap
https://stackoverflow.com/questions/33092942/how-to-convert-nsimage-to-firemonkey-tbitmap
https://forum.lazarus.freepascal.org/index.php?topic=32356.0
https://stackoverflow.com/questions/3038820/how-to-save-a-nsimage-as-a-new-file

Most promising pieces of code are:

Code: Pascal  [Select][+][-]
  1.   NSSize mySize = mapImage.bounds.size;
  2.   NSSize imgSize = NSMakeSize( mySize.width, mySize.height );
  3.   NSBitmapImageRep *bir = [mapImage bitmapImageRepForCachingDisplayInRect:[mapImage bounds]];
  4.   [bir setSize:imgSize];
  5.   [mapImage cacheDisplayInRect:[mapImage bounds] toBitmapImageRep:bir];
  6.   caheImg = [[NSImage alloc]initWithSize:imgSize];
  7.   [caheImg addRepresentation:bir];
  8.  

Code: [Select]
UIGraphicsBeginImageContext(rect.size); // UIGraphicsBeginImageContext(self.view.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Does anyone here already done something like the above in Lazarus?  One issues is Lazarus has not yet converted API in the above examples (e.g. UIGraphicsBeginImageContext and other are not recognized)


Title: Re: Get screnshot of of control/contents (here webview)
Post by: skalogryz on August 05, 2019, 04:21:56 am
UIGraphicsBeginImageContext (and anything UIxxx) is iOS.

Not really part of AppKit (Cocoa).

Since iOS is brought as an "official" part of macOS, it might be possible to have "UIKit" widgetset in future.
Title: Re: Get screnshot of of control/contents (here webview)
Post by: MISV on August 05, 2019, 09:48:47 pm
I have this code, but the results look wrong...

Some part may be because I do not size the webview correctly first... No sure. But the coloring is probably because of the underneath code... Unfortunately. Will post some attachments...

Code: Pascal  [Select][+][-]
  1.    
  2.      TmpNSDataRef := FWebBrowser.FWebView_Cocoa.dataWithPDFInsideRect(FWebBrowser.FWebView_Cocoa.bounds);
  3.       TmpNSImage := NSImage.alloc.initWithData(TmpNSDataRef);
  4.       if TmpNSImage.Representations.Count = 0 then
  5.         Exit
  6.       ;
  7.       TmpNSDataRef := TmpNSImage.TIFFRepresentation;
  8.       TmpTiffImage := TTiffImage.Create;
  9.       TmpMemStream := TmsOverlayFixedMemoryStream.Create;
  10.       TmpMemStream.SetPointer(TmpNSDataRef.Bytes, TmpNSDataRef.Length);
  11.       try
  12.         TmpTiffImage.LoadFromStream(TmpMemStream);
  13.         Result := TBitmap.Create;
  14.         try
  15.           Result.Assign(TmpTiffImage);
  16.         except
  17.           Result.Free;
  18.           Result := nil;
  19.         end;
  20.       finally
  21.         TmpTiffImage.free;
  22.         TmpMemStream.free;
  23.       end;
  24.       TmpNSImage.dealloc;
  25.  

Update

Above code does not even work properly... When called a few times in row
Code: Pascal  [Select][+][-]
  1. TmpNSDataRef := FWebBrowser.FWebView_Cocoa.dataWithPDFInsideRect(FWebBrowser.FWebView_Cocoa.bounds);
causes a bad crash (I think ind the webview widget itself, so not useful callstack)

After a lot trial and error that specific line above seems to be the problem (and not e.g. resizing of the kinda-a offscreen form hosting the webview ... been experimenting quite a bit)

For the screenshots the above does manage to make before crashing they
1) look horrible miscolored (maybe e.g. only red black and white shades)
2) the page is repeated horizontally once (only partially visible but still)
Title: Re: Get screnshot of of control/contents (here webview)
Post by: MISV on August 06, 2019, 04:08:48 pm
Just to give back to this forum who has helped me many times:


Code: Pascal  [Select][+][-]
  1.       TmpNSRect := FWebBrowser.FWebView_Cocoa.bounds;
  2.       TmpNSSize := FWebBrowser.FWebView_Cocoa.bounds.size;
  3.       TmpNSBitmapRep := FWebBrowser.FWebView_Cocoa.bitmapImageRepForCachingDisplayInRect(TmpNSRect);
  4.       TmpNSBitmapRep.setSize(TmpNSSize);
  5.       FWebBrowser.FWebView_Cocoa.cacheDisplayInRect_toBitmapImageRep(TmpNSRect, TmpNSBitmapRep);
  6.       TmpNSData := TmpNSBitmapRep.representationUsingType_properties(NSBMPFileType, nil);
  7.       TmpMemStream := TmsOverlayFixedMemoryStream.Create; // dummy stream that does no allocation/deallocation
  8.       TmpMemStream.SetPointer(TmpNSDataRef.Bytes, TmpNSData.Length);
  9.       Result := TBitmap.Create;
  10.       Result.LoadFromStream(TmpMemStream);
  11.  


Works
TinyPortal © 2005-2018