HtmlDialog #write_image alternative

on a mac,

I can use a builtin system call to take a screenshot of my dlg using it’s position and size…

# screencapture -R top_left_x, top_left_y, width, height, image_path
%x[screencapture -R100,100,1024,1024 /tmp/outfile.jpg]

it seems on windows I could possibly use a gem to achieve the same…

require 'win32/screenshot'

# Take a screenshot of the window with the specified title, client area (e.g. without title bar)
Win32::Screenshot::Take.of(:window, title: 'my_dlg', context: :client)

has anyone tried this gem in SU?


What is the use case for this? To my understanding WebDialog#write_image was a somewhat dirty hack to download map imagery for Geo Location.

I’ve got a couple of image editor extensions that used write_image…

a lot of things I switched to image_rep, but it gets very expensive when all you need is a thumbnail with some text…

I don’t see why you would think it was ever a hack…


I don’t see how it could not be a hack. It makes little sense in itself and must have been added for geo location, which actually relied on it. Rather than “properly” fetching images in the background from a server, it snapped a screenshot, which could include completely different content if you minimized SketchUp at the right (wrong?) time. Sure, it did the job, but it was not very sophisticated nor elegant. Classical hack.

1 Like

Although it installs the gems okay via …

Gem::install "win32screenshot"
#=> [ #<Gem::Specification:0x100086c77a4 ffi-1.11.3-x64-mingw32>, 
  #<Gem::Specification:0x10025b042dc mini_magick-4.10.1>,
  #<Gem::Specification:0x100088437a4 rautomation-1.0.0>, 
  #<Gem::Specification:0x100088e5130 win32screenshot-3.0.0> ]

… it does not work “out of the box” …

require 'win32/screenshot'

… results in …

Error: #<TypeError: no implicit conversion of Sketchup::Console into String>
C:/Program Files/SketchUp/SketchUp 2018/Tools/RubyStdLib/logger.rb:628:in `open'
C:/Program Files/SketchUp/SketchUp 2018/Tools/RubyStdLib/logger.rb:628:in `open_logfile'
C:/Program Files/SketchUp/SketchUp 2018/Tools/RubyStdLib/logger.rb:584:in `initialize'
C:/Program Files/SketchUp/SketchUp 2018/Tools/RubyStdLib/logger.rb:318:in `new'
C:/Program Files/SketchUp/SketchUp 2018/Tools/RubyStdLib/logger.rb:318:in `initialize'
C:/Users/Dan/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Gems64/gems/mini_magick-4.10.1/lib/mini_magick/configuration.rb:97:in `new'
C:/Users/Dan/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Gems64/gems/mini_magick-4.10.1/lib/mini_magick/configuration.rb:97:in `extended'
C:/Users/Dan/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Gems64/gems/mini_magick-4.10.1/lib/mini_magick.rb:6:in `extend'
C:/Users/Dan/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Gems64/gems/mini_magick-4.10.1/lib/mini_magick.rb:6:in `<module:MiniMagick>'
1 Like

Since HtmlDialogs use somewhat “modern” web APIs, I could imagine one does not actually need a SketchUp method for doing this (and not even depend on a system utility hoping that it is available on that OS version and that it finds the right window).

It seems one has to make a “copy” the html of which you want an image in a canvas and then save it as pixel data or file. There certainly exists a library so you don’t have to implement it by hand.

Maybe html2canvas with fileSaver.js:

import html2canvas from "html2canvas";
import { saveAs } from "file-saver";

html2canvas(document.querySelector("body")).then(canvas => {
    canvas.toBlob(blob => saveAs(blob, "pretty image.png"))

I assume for geolocation they needed one complete image (not 20-50 tiles) and including the Google logo (separate html layer). If Google didn’t provide a JSON request API to generate such an image on their servers, then this was the best way to comply with the requirements. Still it’s questionable whether to make it a public Ruby API method if you don’t plan to maintain it forever.


I have some rgba images where converting the pixel data fails in canvas and in image_rep…

I don’t know what app originally created them but the have background gradient that is hidden with alpha set to 0…

heres one…

imageMagic, imageRep, canvas will all return a coloured mask unless I do multi format step conversion…

using a screenshot I get this…