Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling the Rasterize[] width for notebook-related expressions
    text
    copied!<p><strong>Update</strong> Mr Wizard's answer gives pixel-perfect results, but it is Windows-only and destroys the clipboard contents. My answer should work on any platform, but it's less precise: e.g. it omits In/Out labels. It does allow setting the rasterization width though.</p> <hr> <p>This problem came up when I was <a href="https://stackoverflow.com/a/8343460/695132">trying to make a preview window for an image uploader</a> (see the end of that answer).</p> <p>I would like to create a palette button that will upload the current notebook selection as an image. Before uploading, I would like to show a preview of the image, to reduce the chance of something going awry before contacting the server.</p> <p>This is what I have so far (includes only the preview code, not the uploader):</p> <pre><code>button = Button[ "Preview", Module[ {expr = NotebookRead@InputNotebook[]}, If[expr =!= {}, With[{img = Rasterize[expr]}, MessageDialog[ Column[{"Would you like to perform the action?", img}], {"Do it!" :&gt; doIt[img], "Cancel" :&gt; Null} ] ] ] ] ] </code></pre> <p><sup>In case you are wondering why I used a nested <code>With</code> inside the <code>Module</code> instead of making <code>img</code> a module-variable too: it's because by the time <code>doIt[img]</code> is evaluated, the local module variables will have been cleared, so I need to substitute the rasterized expression directly into the <code>doIt</code> function,</sup></p> <p>This button works (more or less). You can try it by creating a graphic in the same notebook (e.g. <code>Graphics[Circle[]]</code>), selecting it using a single click, then clicking the Preview button.</p> <p>However, if I put it in a palette using <code>CreatePalette[button]</code>, then the rasterization will happen for the window-width of the palette, and I get something like this:</p> <p><img src="https://i.stack.imgur.com/d3HzO.png" alt="Screenshot of the problem"></p> <p><strong>How can I control the width of rasterization, or more generally, how can I create a preview dialog for the uploader that avoids this issue?</strong></p> <p>For an additional improvement, it would be nice to be able to size the message window so it fits the preview image (and still shows the button: the button disappears with <code>WindowSize -&gt; All</code>).</p> <hr> <h2>Answers</h2> <p><strong>Mr. Wizard's suggestion:</strong></p> <pre><code>button = Button[ "Preview", (FrontEndExecute[ FrontEndToken[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]]; MessageDialog[ First@Cases[NotebookGet@ClipboardNotebook[], RasterBox[data_, ___] :&gt; Image[data, "Byte", ColorSpace -&gt; "RGB", Magnification -&gt; 1], Infinity]])] CreatePalette[button] </code></pre> <p><strong>Problems:</strong> It (probably) only works on Windows, and it destroys the clipboard contents.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload