SaveGraphImageFile
(
NodeXLControl oNodeXLControl,
IEnumerable<LegendControlBase> oLegendControls,
String sWorkbookFilePath
)
{
Debug.Assert(oNodeXLControl != null);
Debug.Assert(oLegendControls != null);
Debug.Assert( !String.IsNullOrEmpty(sWorkbookFilePath) );
AutomatedGraphImageUserSettings oAutomatedGraphImageUserSettings =
new AutomatedGraphImageUserSettings();
System.Drawing.Size oImageSizePx =
oAutomatedGraphImageUserSettings.ImageSizePx;
Int32 iWidth = oImageSizePx.Width;
Int32 iHeight = oImageSizePx.Height;
Boolean bIncludeHeader = oAutomatedGraphImageUserSettings.IncludeHeader;
Boolean bIncludeFooter = oAutomatedGraphImageUserSettings.IncludeFooter;
Debug.Assert(!bIncludeHeader ||
oAutomatedGraphImageUserSettings.HeaderText != null);
Debug.Assert(!bIncludeFooter ||
oAutomatedGraphImageUserSettings.FooterText != null);
GraphImageCompositor oGraphImageCompositor =
new GraphImageCompositor(oNodeXLControl);
UIElement oCompositeElement = oGraphImageCompositor.Composite(
iWidth, iHeight,
bIncludeHeader ? oAutomatedGraphImageUserSettings.HeaderText : null,
bIncludeFooter ? oAutomatedGraphImageUserSettings.FooterText : null,
oAutomatedGraphImageUserSettings.HeaderFooterFont, oLegendControls
);
System.Drawing.Bitmap oBitmap = WpfGraphicsUtil.VisualToBitmap(
oCompositeElement, iWidth, iHeight);
ImageFormat eImageFormat =
oAutomatedGraphImageUserSettings.ImageFormat;
String sImageFilePath = Path.ChangeExtension( sWorkbookFilePath,
SaveableImageFormats.GetFileExtension(eImageFormat) );
try
{
oBitmap.Save(sImageFilePath, eImageFormat);
}
catch (System.Runtime.InteropServices.ExternalException)
{
// When an image file already exists and is read-only, an
// ExternalException is thrown.
//
// Note that this method is called from the
// ThisWorkbook.GraphLaidOut event handler, so this exception can't
// be handled by a TaskAutomator.AutomateOneWorkbook() exception
// handler.
FormUtil.ShowWarning( String.Format(
"The image file \"{0}\" couldn't be saved. Does a read-only"
+ " file with the same name already exist?"
,
sImageFilePath
) );
}
finally
{
oBitmap.Dispose();
oGraphImageCompositor.RestoreNodeXLControl();
}
}