# Image of input and output cells is not exported correctly

Wolfram Blog uses PNG images to display input and output cells in a notebook. For example, the following is an image from Samuel Chen's post:

I should be able to produce a similar image by selecting a group of cells and selecting File -> Save Selection from the pull-down menu. When I tried that on the cell

In[1]:= Series[Log[1+x],{x,0,6}]
Out[1]= x-x^2/2+x^3/3-x^4/4+x^5/5-x^6/6+O[x]^7


I get the garbled image

This does not happen when I save selection to PDF or BMP. In the case of BMP, the In[1] and Out[1] goes missing:

Do you know of a way to fix the problem with PNG export and/or include the In[1] in BMP export? I am running Mathematica 8.0.4.0 on Linux x86 (64-bit), which I think is part of the problem.

-
How do you know they didn't take a screenshot? –  rm -rf Jun 3 '12 at 7:17
This works correctly for me with v7 on Win7: i.stack.imgur.com/dzJJ1.png –  Mr.Wizard Jun 3 '12 at 7:21
Since I am not seeing this problem, please check your setting for "PageWidth" in the Option Inspector, especially the one for ExportMultipleCellsOptions. Mine is set for Infinity. –  Mr.Wizard Jun 3 '12 at 7:26
@Mr.Wizard The image Szabolcs's uploader produces looks great: i.stack.imgur.com/jRg2e.png. I will look at his code. Thanks! –  Michael Wijaya Jun 3 '12 at 7:38
I ran into a similar problem when making the uploader. The system was using the palette's width to rasterize the selection, not the width of the notebook where the expression was. It's still not completely clear to me how the width is determined. You can google this site for ExportPacket and see this too for some hints. ("The front end can be unpredictable about what line width it uses to compute breaks, so it is usually wise to specify an explicit value for the PageWidth option." –  Szabolcs Jun 3 '12 at 8:18

-
This should only be a comment... –  rm -rf Jun 3 '12 at 8:18
@R.M well, I started with it as a comment but as it appears to at least partially solve the OP's question I promoted it to an answer. If Szabolcs posted a proper method I will delete this. Otherwise, I think it's better to have an answer be an answer, even if it's a poor one. –  Mr.Wizard Jun 3 '12 at 8:38
Well here we are, four and a half months later, and still only my crummy answer, while Szabolcs has apparently left StackExchange for the married life. –  Mr.Wizard Oct 17 '12 at 0:48
My views haven't changed though :) –  rm -rf Oct 17 '12 at 7:08
@rm-rf I'd be disappointed if they had, my verbal sparing partner. :o) –  Mr.Wizard Oct 17 '12 at 7:11

First, I cannot reproduce the problem by using File -> Save As..., but I have seen similar things while developing the first (now defunct) version of the image uploader.

The first approach I tried for rasterizing parts of a notebook was something along the lines of Rasterize[NotebookSelection[]]. I discovered that if I put this into a button, and put the button into a palette, everything will be reformatted to the width of the palette for rasterization.

I used two workarounds:

1. Copy the selection into a new hidden notebook (CreateDocument[..., Visible -> False]) and Rasterize the whole notebook. The width of the hidden notebook defines the rasterization width.

2. Use Edit -> Copy As -> Bitmap (use it programmatically through FrontEndTokenExecute), then paste back the bitmap into a notebook for exporting. This is available only on Windows (any version) or Mac (version 9 only).

3. There's a third possibility using ExportPacket, described in this answer and its comments. I couldn't use this in the image uploader because it only works if a complete cell is selected by clicking on the cell bracket. It can't be used if a cell is only partially selected.

You can start along these lines to fix the problem. If you get stuck, let me know, and I'll go into details.

-

I just saw the question now. To add to Szabolcs' very detailed and general answer something practical, which works under Linux with Mathematica 8.0.4, let me give you code (a function from Szabolcs' image uploader), which works directly:

rasterize[maxWidth_: 650] :=
Module[{target, selection, image},
If[MemberQ[Hold[{}, $Failed, NotebookRead[$Failed]],
selection], \$Failed,(*there was nothing selected*)
target =
CreateDocument[{}, WindowSelected -> False, Visible -> False,
WindowSize -> maxWidth];
NotebookWrite[target, selection];
image = Rasterize[target, "Image"];
NotebookClose[target];
Print[image]]];

Button["Rasterize Selection", rasterize[] &]


Basically, you can do whatever you like with the image at the end, but for simplicity, I just print it so you get it in the notebook. When you select your complete cell and press the button you get the following image

-