Update 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.
This problem came up when I was trying to make a preview window for an image uploader (see the end of that answer).
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.
This is what I have so far (includes only the preview code, not the uploader):
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!" :> doIt[img], "Cancel" :> Null}
]
]
]
]
]
In case you are wondering why I used a nested With
inside the Module
instead of making img
a module-variable too: it's because by the time doIt[img]
is evaluated, the local module variables will have been cleared, so I need to substitute the rasterized expression directly into the doIt
function,
This button works (more or less). You can try it by creating a graphic in the same notebook (e.g. Graphics[Circle[]]
), selecting it using a single click, then clicking the Preview button.
However, if I put it in a palette using CreatePalette[button]
, then the rasterization will happen for the window-width of the palette, and I get something like this:
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?
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 WindowSize -> All
).
Answers
Mr. Wizard's suggestion:
button = Button[
"Preview", (FrontEndExecute[
FrontEndToken[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]];
MessageDialog[
First@Cases[NotebookGet@ClipboardNotebook[],
RasterBox[data_, ___] :>
Image[data, "Byte", ColorSpace -> "RGB", Magnification -> 1],
Infinity]])]
CreatePalette[button]
Problems: It (probably) only works on Windows, and it destroys the clipboard contents.
I think this should work without you needing to make a new notebook:
I used a little option searcher to find
ImageFormattingWidth
and by passing the image width as the window width you can make the dialog fit the picture nicely and still display the button.Here's a demo of its results:
Rasterizing a cell from a pallete
I managed to do this by copying the selection to a new notebook, rasterizing the full notebook, then closing it.
The
WindowSize -> 500
option can be added toCreateDocument
to set the rasterization width to 500 pixels.Note some disadvantages (advantages in some cases) of this method compared to copying as bitmap:
If there's a need, some of these can be remedied by explicitly transferring some notebook options from the
SelectedNotebook
to the newly created one.Have you tried using ExportString[] to create the graphic into memory? (technically into a temp file, but what do you care:] )
See the output on a colored background to verify transparent BG:
For images with many color variations (like 3D plots), I recommend JPEG2000 format, and for solid-colored images where transparency is not necessary, use GIF to preserve color detail.
Yes, you can control ImageSize when you export string of the image.
ExportString output and image type/size comparison http://i44.tinypic.com/ok6fl.png
If it is practical to use the clipboard in this operation you might use:
FrontEnd`CopySpecial["MGF"]
(copy as bitmap).