Smrf.NodeXL.ExcelTemplate.SaveGraphImageFileDialog.SaveObject C# (CSharp) Method

SaveObject() protected method

protected SaveObject ( Object oObject, String sFileName ) : void
oObject Object
sFileName String
return void
    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();
            }
        }
    }