SaveGraphImageFile ( NodeXLControl oNodeXLControl, String sWorkbookFilePath ) { Debug.Assert(oNodeXLControl != null); Debug.Assert(!String.IsNullOrEmpty(sWorkbookFilePath)); AutomatedGraphImageUserSettings oAutomatedGraphImageUserSettings = new AutomatedGraphImageUserSettings(); Size oImageSizePx = oAutomatedGraphImageUserSettings.ImageSizePx; Bitmap oBitmapCopy = oNodeXLControl.CopyGraphToBitmap( oImageSizePx.Width, oImageSizePx.Height); ImageFormat eImageFormat = oAutomatedGraphImageUserSettings.ImageFormat; String sImageFilePath = Path.ChangeExtension(sWorkbookFilePath, SaveableImageFormats.GetFileExtension(eImageFormat)); try { oBitmapCopy.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.AutomateThisWorkbook() 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 { oBitmapCopy.Dispose(); } }
btnSaveGraphImageFile_Click ( object sender, EventArgs e ) { AssertValid(); AutomatedGraphImageUserSettings oAutomatedGraphImageUserSettings = new AutomatedGraphImageUserSettings(); AutomatedGraphImageUserSettingsDialog oAutomatedGraphImageUserSettingsDialog = new AutomatedGraphImageUserSettingsDialog( oAutomatedGraphImageUserSettings); if (oAutomatedGraphImageUserSettingsDialog.ShowDialog() == DialogResult.OK) { oAutomatedGraphImageUserSettings.Save(); } }
//************************************************************************* // Constructor: AutomatedGraphImageUserSettingsDialog() // /// <summary> /// Initializes a new instance of the <see /// cref="AutomatedGraphImageUserSettingsDialog" /> class. /// </summary> /// /// <param name="automatedGraphImageUserSettingsUserSettings"> /// The object being edited. /// </param> //************************************************************************* public AutomatedGraphImageUserSettingsDialog ( AutomatedGraphImageUserSettings automatedGraphImageUserSettingsUserSettings ) { Debug.Assert(automatedGraphImageUserSettingsUserSettings != null); automatedGraphImageUserSettingsUserSettings.AssertValid(); m_oAutomatedGraphImageUserSettings = automatedGraphImageUserSettingsUserSettings; // Instantiate an object that saves and retrieves the position of this // dialog. Note that the object automatically saves the settings when // the form closes. m_oAutomatedGraphImageUserSettingsDialogUserSettings = new AutomatedGraphImageUserSettingsDialogUserSettings(this); InitializeComponent(); DoDataExchange(false); AssertValid(); }