Diagram for .NET / User's Guide / Extensions / Image Exporter

Image Exporter
The image exporter extension can help you export a drawing as a raster image. It is represented by the NImageExporter class whose one and only constructor by design requires a reference to the document, which must be exported to image.
C#
Copy Code
// create a new image exporter
NImageExporter imageExporter = new NImageExporter(document);
Visual Basic
Copy Code
' create a new image exporter
Dim imageExporter As New NImageExporter(document)
 Export bounds

The export bounds are the bounds in scene coordinates, which are exported to image. You specify them with the help of the ExportBounds property, which is by default set to the document bounds. The KnownBoundsTable property gives you access to a hashtable in which the name of the export bounds is the key and the value is the bounds which must be exported. The content of this table is used in the visual editor of the image exporter (shown by the ShowDialog method). For example:

C#
Copy Code
// export the scene area with the specified rectangle
imageExporter.ExportBounds = new NRectangleF(100, 100, 200, 200);
Visual Basic
Copy Code
' export the scene area with the specified rectangle
imageExporter.ExportBounds = New NRectangleF(100, 100, 200, 200)
 Resolution and pixel format

The resolution of the generated image is controlled by the Resolution property. By default it is initialized with the document resolution. The pixel format is controlled by the PixelFormat property - by default set to Format24bppRgb. 

C#
Copy Code
// generate a printing quality image
imageExporter.Resolution = 300;

// use Format24bppRgb pixel form
imageExporter.PixelFormat = PixelFormat.Format24bppRgb;
Visual Basic
Copy Code
' generate a printing quality image
imageExporter.Resolution = 300

' use Format24bppRgb pixel form
imageExporter.PixelFormat = PixelFormat.Format24bppRgb
 Graphics settings and gray scale

You can control the settings of the graphics used for image export via the GraphicsSettings property. A grayscaled image can be created by setting the GrayScale property to true:

C#
Copy Code
// instruct the image exporter to generate an antialiased grayscale image
imageExporter.GrayScale = true;
imageExporter.GraphicsSettings.SmoothingMode = SmoothingMode.AntiAlias;
Visual Basic
Copy Code
' instruct the image exporter to generate an antialiased grayscale image
imageExporter.GrayScale = True
imageExporter.GraphicsSettings.SmoothingMode = SmoothingMode.AntiAlias
 Image export

Once you have setup the image exporter you can perform the following operations:

  • Save the image to an image file - by calling the SaveToImageFile method. For example:
    C#
    Copy Code
    // export the document to a png image
    imageExporter.SaveToImageFile("c:\\temp\\myimage.png", ImageFormat.Png);
    
    Visual Basic
    Copy Code
    ' export the document to a png image
    imageExporter.SaveToImageFile("c:\\temp\\myimage.png", ImageFormat.Png)
    
  • Render the image to a memory bitmap - by calling the RenderToBitmap method. For example:
    C#
    Copy Code
    // render a memory bitmap
    Bitmap bmp = imageExporter.RenderToBitmap();
    
    Visual Basic
    Copy Code
    ' render a memory bitmap
    Dim bmp as Bitmap = imageExporter.RenderToBitmap()
    
  • Copy the image in the clipboard - by calling the CopyToClipboard method. For example:
    C#
    Copy Code
    // render the document in a bitmap and place it in the clipboard
    imageExporter.CopyToClipboard();
    
    Visual Basic
    Copy Code
    ' render the document in a bitmap and place it in the clipboard
    imageExporter.CopyToClipboard()
    
 Related Examples
Windows Forms:  Extensions - Image Exporter 
See Also