Snapshot of HTMLViewer

Shouldn’t this work?

I want to specifiy a URL… and take a picture (size specified by w,h)
I get no errors… HTMLViewer has the webpage… but P is blank

  HTMLViewer1.LoadURL("http://www.google.com")
  p=new picture(w,h,32) 
  HTMLViewer1.DrawInto(p.graphics,0,0)

No
HTMLViewer’s don’t draw into like most other controls
It could be reasonably argued this is a bug

It may even have been reported

Yeah… since posting… I found another topic that had some workarounds… am trying to see if I can make it work for my situation

If you’re on a Mac, I started using the command line utility screencapture via Applescript. It’s not mentioned in the man pages but there’s a -R switch that will capture a rectangle of the screen.

Applescript:

on run {x, y, w, h} set str to (x & ", " & y & ", " & w & ", " & h) as string do shell script ("screencapture -x -c -R '" & str & "'") as string end run

calling from Xojo:

capture_screen_area(htmlviewer1.Left + window1.left, htmlviewer1.top + window1.top, htmlviewer1.Width, htmlviewer1.Height) dim c as new Clipboard temp_pic = c.Picture c.close

My usage here is for an app only I will use so I’m sending the screen shot to the clipboard but you can just as easily send it to a temp file.

That won’t work… main reason I am not displaying the HTMLViewer to the user…
They have a rectangle they can drag around the screen … and I thought it would enhance the app if they could see a thumbnail of the URL they associated to it … but HTMLViewer has to be VISIBLE for screen capture to work…

Well, first you have to wait after LoadURL for the webviewer to actually load something, so make a pause.
Maybe check in DocumentComplete event.

Next you can take a screenshot.

But even better maybe to use our MBS Plugin functions to render the image as a picture object. And not just render the visible area, but the whole page.

Yes … I realize that… but you cannot take a screenshot of a non-visible control…

and you know my position on plugins…

Without having the webviewer visible, it will not render.
Believe me, I tried it.

Maybe you can find this open source webkit console app for rendering websites. I forgot the name, maybe in archives here?

I know it won’t … I never said it would… this is not an important enough issue to pursue any more… thank you.

What about opening a window offscreen and using the declares from the other thread?

It would steal focus (however briefly) from the user… and they would be right at the start of a mousedrag… and that would screw it all up…

I’m just going to go without… its was a nice to have… not an requirement

Is there any news on this subject?
I want to use the htmlviewer.drawinto method to create a jpeg of the visible part of the webpage.
This may have been reportet as a bug four years ago.
Is this issue fixed now?
There are several workarounds in this forum, none of them is working properly on my mac with google maps.

Did you try HTMLViewer.DrawInto ? I don’t think anything has changed with it though… :-/

What version of Xojo?
32-bit, 64-bit, both?
What version of macOS?

[quote=389511:@Mario Schweda]Is there any news on this subject?
I want to use the htmlviewer.drawinto method to create a jpeg of the visible part of the webpage.
This may have been reportet as a bug four years ago.
Is this issue fixed now?
There are several workarounds in this forum, none of them is working properly on my mac with google maps.[/quote]

Take a look here Xojo post and here wkhtmltoimage

You could also use phantomJS or phantomjscloud.com

wow… so many answers… thank you…

[quote=389549:@shao sean]Did you try HTMLViewer.DrawInto ? I don’t think anything has changed with it though… :-/

What version of Xojo?
32-bit, 64-bit, both?
What version of macOS?[/quote]

Both versions, Mac 10.13.4

I’m using wkhtmltoimage for some of my bash scripts and for perl.
Especially for xojo I didn’t want to use workarounds because the function is available in Xojo but not working.

At the end i’m ending up using a workaround where I create the image with googleapi.destination and then I’m creating a static map with googleapi.staticmaps.

I was looking into that. I guess this is not the right choice for my program.
Nice job with your xojo controls, by the way. I’m using the chart view controls all the time… it’s awesome.

This code works well for me in 10.11 thru 10.13. Requires MBS plugins.


Attributes( StructureAlignment = 1 ) Protected Structure NSRect
  x as CGFloat
  y as CGFloat
  width as CGFloat
  height as CGFloat
End Structure


Function GetScreenshot ( extends w as Window ) as Picture 
#if TargetCocoa

    dim captureRect as NSRect
    captureRect.x = 0
    captureRect.y = 0
    captureRect.width = w.width
    captureRect.height = w.height
    
    dim nsv as NSViewMBS = w.TrueWindow.NSWindowMBS.contentView
    declare function bitmapImageRepForCachingDisplayInRect lib kLibCocoa selector "bitmapImageRepForCachingDisplayInRect:" (obj_id as integer, rect as NSRect) as Ptr
    dim rep as Ptr = bitmapImageRepForCachingDisplayInRect(nsv.Handle, captureRect)
    declare sub cacheDisplayInRect lib kLibCocoa selector "cacheDisplayInRect:toBitmapImageRep:" (obj_id as Integer, rect as NSRect, rep as Ptr)
    
    cacheDisplayInRect(nsv.handle,captureRect,rep)  // WARNING: this may cause a Window.Paint event
    
    declare function CGImage lib kLibAppKit selector "CGImage" ( NSBitmapImageRep as Ptr ) as Ptr
    dim cgPtr as Ptr = CGImage( rep )
    dim cgi as CGImageMBS = CGImageMBS.CreateImageWithHandle(integer(cgPtr))
    dim pic as Picture = cgi.Picture
    return pic
#endif
end function

@Michael Diehr

thank you

unfortunately I’m not using the MBS plugins.

You can download it here:

http://www.monkeybreadsoftware.de/xojo/download.shtml

And if you like get a trial license:

http://www.monkeybreadsoftware.de/trial/trial.shtml