Get an image of the view displayed in the editor
To get an image of the view displayed in the editor, use the GetImage
method of the IEditorView
object.
The following is an example of getting an image from the currently displayed editor and saving the image in BMP format.
public void GetImage(ICommandContext c, ICommandParams p)
{
//Set the destination file path
string outputFilePath = c.App.Window.UI.ShowSaveFileDialog("Destination file path", "bmp file (*.bmp)|*.bmp");
if(string.IsNullOrEmpty(outputFilePath))
{
//End processing if the destination file path cannot be obtained
c.App.Output.WriteLine("sample", "Destination file path not set.");
return;
}
//Get the image from the diagram.
IEditorView editorView = c.App.Window.EditorPage.CurrentEditorView;
System.Windows.Media.Imaging.BitmapSource bitmapSource = editorView.GetImage();
if (bitmapSource == null)
{
//If no diagram is displayed, terminate processing.
c.App.Output.WriteLine("sample", "Could not obtain image of view from displayed editor.");
return;
}
//Save the obtained image in BMP format to the output file path.
using (System.IO.FileStream fileStream = new System.IO.FileStream(outputFilePath, System.IO.FileMode.Create))
{
System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
encoder.Save(fileStream);
}
c.App.Output.WriteLine("sample", $"Image saved to {outputFilePath}.");
}
If you want to save an image in PNG format, you can use the SaveImage
method of the IEditorView
object.
Below is an example of saving an image of the currently displayed editor.
public void SaveImage(ICommandContext c, ICommandParams p)
{
//Set the destination file path
string outputFilePath = c.App.Window.UI.ShowSaveFileDialog("Destination file path", "png file (*.png)|*.png");
if(string.IsNullOrEmpty(outputFilePath))
{
//End processing if the destination file path cannot be obtained
c.App.Output.WriteLine("sample", "Destination file path not set.");
return;
}
//Save the diagram image.
IEditorView editorView = c.App.Window.EditorPage.CurrentEditorView;
editorView.SaveImage(outputFilePath);
c.App.Output.WriteLine("sample", $"Image saved to {outputFilePath}.");
}