Composites a NodeXLControl and other elements into a UIElement.
Call the Composite method to composite a NodeXLControl with other elements into a new UIElement. Call when you are done with the UIElement. The UIElement can be used to create an image that can be saved to disk.
Inheritance: Object
        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();
        }
    }
    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();
            }
        }
    }
示例#4
0
        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();
            }
        }