##### Page tree

The MarvinSketch canvas is referred to as the area where all molecule fragments and graphical objects such as arrows, text boxes, etc. appear.
The scale factor in MarvinSketch determines the displayed size of a Carbon-Carbon single bond measured in pixels.

The magnification is the value in percents that indicate the actual zoom ratio of the contents of the canvas.
The default value of the scale factor is 28, and this is equivalent to 100% magnification.

This example demonstrates how to create images from the contents of the MarvinSketch canvas using two different approaches.

The image below shows the sample GUI component of this example.

There are two buttons below the MSketchPane component, Get Sketch Image and Get Molecule Image, that will demonstrate the difference between the two methods.

To make it easy, the images that are created will be shown by placing them on a javax.swing.JLabel component below the two buttons.

By pressing the Get Sketch Image button, the image of the canvas using the actual magnification is generated and set as the icon image of the label.

The Get Molecule Image button creates a 200 x 200 pixel size image, automatically computing the appropriate scale factor. In this example we modify the default behavior with a parameter. If this scale factor is greater than 100%, the scale factor is explicitly set to 100%, so that small molecules will not be overscaled. Note, that the molecule is automatically centered in the image.

The very brief code samples below are taken from SketchImages.java.

#### Creating the image of the canvas

The MSketchPane bean can generate an image from the contents of the canvas if either a scale factor or an absolute image size (with java.awt.Dimension) is set. In this example we use the actual scale factor of the canvas itself, thus zooming affects the result of the generated image.

    // getting the actual scale factor of the canvas
double currentScale = sketchPane.getScale();
// getting the image of the canvas with the current scale factor
Image canvasImage = sketchPane.getImage(currentScale);


#### Converting the molecule of the canvas to an image

The image format along with additional options should be set as a String. The options are separated from the format identifier with a colon, and from each other with commas.
More details of specifying image formats are available in the Image Generation documentation.

With the use of the maxscale parameter the scale factor is not allowed to be grater than that of 100%.

    String format = "jpeg:w200,h200,maxscale28";
byte[] imageData = sketchPane.getMol().toBinFormat(format);


As of Marvin 5.2.0 images can be created with transparent background in PNG, SVG, PDF and EMF formats. To set the transparent background, the transbg parameter should be set along with a background color containing alpha values in RGBA form.

    String format = "png:w200,h200,maxscale28,transbg,#ffffffff";


There is an alternative to the image generation method which does not require to set the image format explicitely, and retrieves a java.awt.Image object instead of the image data. This conversion is more appropriate in case the image is not written to file.

    Image image = sketchPane.getMol().toObject("image:w200,h200");


#### Comparison of the two methods

The advantages of converting the molecule to an image format are straightforward:

• Bean creation is not necessary, it can even be used as a command line method.
• Arbitrary image size setting is possible considering memory limits.

However there is one disadvantage as well, namely that the various functionalities offered by the JavaBean component affecting the display are not directly available on the molecule itself. This means that all display options being different from the default has to be set explicitely as the parameter of the conversion String.

For example, to convert a molecule to an image where the CHIRAL flags are visible is as follows:

    String format = "jpeg:w200,h200,chiral_all";
byte[] imageData = sketchPane.getMol().toBinFormat(format);


The available options are enumerated as Export options of the Image Generation documentation.