CreateSubgraphImage ( IGraph oSubgraph, CreateSubgraphImagesAsyncArgs oCreateSubgraphImagesAsyncArgs, Size oImageSizePx ) { Debug.Assert(oSubgraph != null); Debug.Assert(oCreateSubgraphImagesAsyncArgs != null); AssertValid(); Rectangle oSubgraphRectangle = new Rectangle(new Point(0, 0), oImageSizePx); // Lay out the graph, then draw it using the NodeXLVisual object. IAsyncLayout oLayout = oCreateSubgraphImagesAsyncArgs.Layout; oLayout.LayOutGraph(oSubgraph, new LayoutContext(oSubgraphRectangle)); NodeXLVisual oNodeXLVisual = oCreateSubgraphImagesAsyncArgs.NodeXLVisual; GraphDrawingContext oGraphDrawingContext = CreateGraphDrawingContext(oSubgraphRectangle, oLayout.Margin, oCreateSubgraphImagesAsyncArgs.GeneralUserSettings); oNodeXLVisual.GraphDrawer.DrawGraph(oSubgraph, oGraphDrawingContext); // Save the graph to a bitmap. Bitmap oBitmap = WpfGraphicsUtil.VisualToBitmap(oNodeXLVisual, oSubgraphRectangle.Width, oSubgraphRectangle.Height); return(oBitmap); }
SaveObject ( Object oObject, String sFileName ) { Debug.Assert(oObject is GraphImageInfo); Debug.Assert(!String.IsNullOrEmpty(sFileName)); GraphImageInfo oGraphImageInfo = (GraphImageInfo)oObject; Int32 iWidth = oGraphImageInfo.Width; Int32 iHeight = oGraphImageInfo.Height; GraphImageCompositor oGraphImageCompositor = new GraphImageCompositor(oGraphImageInfo.NodeXLControl); UIElement oCompositeElement = null; try { if (m_oSaveFileDialog.FilterIndex <= SaveableImageFormats.ImageFormats.Length) { // The graph must be saved as a bitmap. oCompositeElement = oGraphImageCompositor.Composite( iWidth, iHeight, oGraphImageInfo.HeaderText, oGraphImageInfo.FooterText, oGraphImageInfo.HeaderFooterFont, oGraphImageInfo.LegendControls); System.Drawing.Bitmap oBitmap = WpfGraphicsUtil.VisualToBitmap( oCompositeElement, iWidth, iHeight); base.SaveObject(oBitmap, sFileName); oBitmap.Dispose(); } else { // The graph must be saved as an XPS. Double documentWidth = ToWpsUnits(iWidth); Double documentHeight = ToWpsUnits(iHeight); oCompositeElement = oGraphImageCompositor.Composite( documentWidth, documentHeight, oGraphImageInfo.HeaderText, oGraphImageInfo.FooterText, oGraphImageInfo.HeaderFooterFont, oGraphImageInfo.LegendControls); Size oDocumentSize = new Size(documentWidth, documentHeight); Rect oDocumentRectangle = new Rect(new Point(), oDocumentSize); FixedDocument oFixedDocument = new FixedDocument(); oFixedDocument.DocumentPaginator.PageSize = oDocumentSize; PageContent oPageContent = new PageContent(); FixedPage oFixedPage = new FixedPage(); oFixedPage.Width = documentWidth; oFixedPage.Height = documentHeight; oFixedPage.Children.Add(oCompositeElement); ((System.Windows.Markup.IAddChild)oPageContent).AddChild( oFixedPage); oFixedDocument.Pages.Add(oPageContent); XpsDocument oXpsDocument = new XpsDocument(sFileName, FileAccess.Write); XpsDocumentWriter oXpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(oXpsDocument); oXpsDocumentWriter.Write(oFixedDocument); oXpsDocument.Close(); } } finally { if (oCompositeElement != null) { oGraphImageCompositor.RestoreNodeXLControl(); } } }
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(); } }