示例#1
0
        SaveSubgraphImage
        (
            Bitmap oBitmap,
            String sFolder,
            String sFileNameNoExtension,
            CreateSubgraphImagesAsyncArgs oCreateSubgraphImagesAsyncArgs
        )
        {
            Debug.Assert(oBitmap != null);
            Debug.Assert(!String.IsNullOrEmpty(sFolder));
            Debug.Assert(!String.IsNullOrEmpty(sFileNameNoExtension));
            Debug.Assert(oCreateSubgraphImagesAsyncArgs != null);
            AssertValid();

            ImageFormat eImageFormat = oCreateSubgraphImagesAsyncArgs.ImageFormat;

            String sFileNameWithoutPath =
                FileUtil.EncodeIllegalFileNameChars(sFileNameNoExtension)
                + "." + SaveableImageFormats.GetFileExtension(eImageFormat);

            String sFileNameWithPath = Path.Combine(sFolder, sFileNameWithoutPath);

            SaveBitmap(oBitmap, sFileNameWithPath, eImageFormat);

            return(sFileNameWithoutPath);
        }
示例#2
0
        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();
            }
        }
        SaveSubgraphImage
        (
            Bitmap oBitmap,
            String sFolder,
            String sVertexName,
            CreateSubgraphImagesAsyncArgs oCreateSubgraphImagesAsyncArgs
        )
        {
            Debug.Assert(oBitmap != null);
            Debug.Assert(!String.IsNullOrEmpty(sFolder));
            Debug.Assert(!String.IsNullOrEmpty(sVertexName));
            Debug.Assert(oCreateSubgraphImagesAsyncArgs != null);
            AssertValid();

            ImageFormat eImageFormat = oCreateSubgraphImagesAsyncArgs.ImageFormat;

            // The "Img-" prefix is to prevent a vertex name like "con" or "lpt"
            // from causing the following exception when
            // System.Drawing.Image.Save() is called:
            //
            //   [ExternalException]: A generic error occurred in GDI+.
            //
            // The exception occurs because "con" (console), "lpt" (line printer)
            // and some other names are reserved file names in Windows.

            String sFileNameNoPath = String.Format(

                "Img-{0}.{1}"
                ,
                FileUtil.EncodeIllegalFileNameChars(sVertexName),
                SaveableImageFormats.GetFileExtension(eImageFormat)
                );

            String sFileNameWithPath = Path.Combine(sFolder, sFileNameNoPath);

            SaveBitmap(oBitmap, sFileNameWithPath, eImageFormat);

            return(sFileNameNoPath);
        }
示例#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();
            }
        }