Drawing Raster Image Export
In This Topic
The NDrawingRasterImageExporter lets you export the content of the active page (or portions of it) to a raster image in the following file formats:
- Portable Network Graphics (PNG)
- JPEG File Interchange Format (JPEG)
- Bitmap (BMP)
- Nevron Raster Image (NRI)
Saving with a Dialog
The following code shows how to display the default raster image export dialog provided by NOV:
Showing the default raster image export dialog |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.ShowDialog(DisplayWindow, true);
|
The Image Options button at the bottom left side of the dialog lets the user configure the color type and compression for PNG images (the ColorType and CompressionLevel properties) and the quality for JPEG images (the Quality property). The default settings are stored in the NPngEncoderSettings.Default and NJpegEncoderSettings.Default. You can change their properties or set them to null if you want to hide the Image Options button from the dialog.
The following code shows a save image file dialog that allows the user to create an image from the active page content:
Saving with a Save File dialog |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.SaveAsImage(NImageFormat.Png); // Save to image with PNG file format selected by default
|
Saving to a File or a Stream
To export the active page of a drawing to image and save it directly to a file, without showing a save file dialog, use the SaveToFile method. Note that the saving to file is an asynchronous (async) operation, so if you are interested in its completion, you should call the Then method of the promise it returns and handle the completion of the export in the passed delegate. The following is a code example:
Saving to a file |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.SaveToFileAsync("C:\\ExportedImage.png").Then(
delegate (NUndefined ud)
{
// Export to PNG completed
}
);
|
Exporting a drawing to an arbitrary file works only on non-sandboxed platforms like WinForms, WPF and Xamarin.Mac.
To export the active page of the drawing to an image stream, use the SaveToStream method. Unlike the SaveToFile method, the SaveToStream method runs synchronously and blocks code execution until it completes. Exporting drawings to stream is useful when you wish to generate images in memory instead of saving them to the disc. The following code snippet shows how to export the active page of a drawing to a stream:
Saving to a stream |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.SaveToStream(stream, NImageFormat.Png);
|
Optionally, you can pass encoder settings for PNG and JPEG images. For example, the following code snippet sets the quality of the exported JPEG image to 80%:
Set JPEG image quality |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.SaveToStream(stream, NImageFormat.Jpeg, new NJpegEncoderSettings(80));
|
Generating a Raster Image
To generate an image on the fly without saving it, use the CreateImage method:
Generating Image |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
NImage image = imageExporter.CreateImage();
|
Copying to Clipboard
To copy the image to the clipboard, use the CopyToClipboard method:
Generating Image |
Copy Code
|
NDrawingRasterImageExporter imageExporter = new NDrawingRasterImageExporter(drawingView.Content);
imageExporter.CopyToClipboard();
|
See Also