Visio Insights
The official blog of the Microsoft Visio product team

New Visio 2010 API for Configuring Raster Export

New Visio 2010 API for Configuring Raster Export

  • Comments 4

When we talk to developers to get their feedback on the Visio development platform, a common request we hear is the ability, through the API, to control the settings used when exporting a diagram as a raster image in the PNG, JPG, BMP, or TIFF format. Typically, they want to automate the export of the pages in a document as images for use in a web page or other application that requires certain settings. In this post, we’ll talk about the additions to the Visio 2010 object model that let developers configure each of these raster export settings.

When you save a diagram in one of the raster formats, a dialog box appears to let you configure various settings, such as the color format, resolution, or size of the image. For example, this is the dialog for the PNG format:

image

Until now, there hasn’t been a way to automate the settings in this dialog via the API. In Visio 2010, we’ve enhanced the object model to make it possible to drive these settings programmatically using a set of new methods and properties on the ApplicationSettings object. You can use the methods to get and set the raster-export resolution and size, and you can use the properties to get and set raster export attributes such as data format, data compression, color reduction, color format, background color, transparency, and quality. You can use these methods and properties with the existing Page.Export and Selection.Export methods to export a diagram in a raster file format.

Here is the complete list of new raster export methods and properties on ApplicationSettings:

Methods

Name

Description

GetRasterExportResolution

Returns the raster export resolution settings.

GetRasterExportSize

Gets the raster export size.

SetRasterExportResolution

Specifies the raster export resolution settings.

SetRasterExportSize

Sets the raster export size.

 

Properties

Name

Description

RasterExportBackgroundColor

Determines the background color that is applied to the exported image.

RasterExportColorFormat

Determines the color format that is applied to the exported image.

RasterExportColorReduction

Determines the color reduction that is applied to the exported image.

RasterExportDataCompression

Determines the data compression algorithm that is applied to the exported image (BMP, TIFF).

RasterExportDataFormat

Determines whether the exported image is interlaced or non-interlaced (PNG, GIF).

RasterExportFlip

Determines the flip that is applied to the exported image.

RasterExportOperation

Determines the export operation that is applied to the exported image (JPG only).

RasterExportQuality

Determines the export quality that is applied to the exported image (JPG only).

RasterExportRotation

Determines the rotation that is applied to the exported image.

RasterExportTransparencyColor

Determines the transparency color that is applied to the exported image (PNG, GIF).

RasterExportUseTransparencyColor

Determines whether Visio applies, to the exported image, the transparency color that is specified in the RasterExportTransparencyColor property (PNG, GIF).

For more details on these methods and properties, such as their parameters and constants, see the Visio Developer Reference in the Visio 2010 Beta.

Example

This VBA code executes the settings in the PNG Output Options dialog box as shown above and exports the diagram as a PNG:

'Set the export resolution to the printer resolution at 600 x 600 pixels/inch
Application.Settings.SetRasterExportResolution visRasterUsePrinterResolution, 600#, 600#, visRasterPixelsPerInch

'Set the export size to custom 8 x 5 inches
Application.Settings.SetRasterExportSize visRasterFitToCustomSize, 8#, 5#, visRasterInch

'Set the data format to Interlace
Application.Settings.RasterExportDataFormat = visRasterInterlace

'Set the color format to 24-bit color
Application.Settings.RasterExportColorFormat = visRaster24Bit

'Rotate the image to the left
Application.Settings.RasterExportRotation = visRasterRotateLeft

'Don’t flip the image
Application.Settings.RasterExportFlip = visRasterNoFlip

'Set the background color
Application.Settings.RasterExportBackgroundColor = 14798527

'Set the transparency color
Application.Settings.RasterExportTransparencyColor = 13269045

'Use the transparency color
Application.Settings.RasterExportUseTransparencyColor = True

'Export the active page as a PNG to the specified path
Application.ActiveWindow.Page.Export "<drive>:\<path>\Network Diagram.png"

We hope these additions to the API for raster export open up some new possibilities for Visio developers, or at least make it easier and more efficient to build automated solutions. Let us know what you think by commenting on the blog or using Send a Smile.

Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post
  • Thank you for listening to the suggestion of the user.

    It will be very helpful.

  • Great!

    Saves us having to supply third party document converters (TIFF G4)

  • Hi,

    I am working with this at the moment, using VB.NET in VS2010. I can see all the Visio objects and have managed to get my code to read a load of XML data, create a workflow diagram, lay it out, theme it and now I want to save it and this API sounds perfect! However, I cannot find the API that you are referring to here. I have Application.Settings but no Raster members whatsoever. Can you give me any clues as to what I might need to do?

    Many thanks,

    Rob.

  • I am trying to export a page to png with the following code. The problem is that documents with Swim lanes doesnt show up and in a diagram with a pool of lanes the pool is shown while the lanes just disappear.

    Please let me know if this can be fixed ?

    Thanks in advance

    CODE:

    visApplication.Settings.SetRasterExportResolution(Visio.VisRasterExportResolution.visRasterUseScreenResolution, 0, 0, Visio.VisRasterExportResolutionUnits.visRasterPixelsPerInch);

                       visApplication.Settings.SetRasterExportSize(Visio.VisRasterExportSize.visRasterFitToSourceSize, 1.020833, 0.520833, Visio.VisRasterExportSizeUnits.visRasterInch);

                       visApplication.Settings.RasterExportColorFormat = Visio.VisRasterExportColorFormat.visRaster24Bit;

                       visApplication.Settings.RasterExportDataFormat = Visio.VisRasterExportDataFormat.visRasterInterlace;

                       visApplication.Settings.RasterExportOperation = Visio.VisRasterExportOperation.visRasterBaseline;

                       visApplication.Settings.RasterExportRotation = Visio.VisRasterExportRotation.visRasterNoRotation;

                       visApplication.Settings.RasterExportFlip = Visio.VisRasterExportFlip.visRasterNoFlip;

                       visApplication.Settings.RasterExportBackgroundColor = 16777215;

                       visApplication.Settings.RasterExportTransparencyColor = 16777215;

                       visApplication.Settings.RasterExportUseTransparencyColor = false;

                       visPage.Export(fileNameAndPathWithoutExt + ".png");

Page 1 of 1 (4 items)