예제 #1
0
파일: Canvas.cs 프로젝트: carlcnx/EnClass
 void IDocumentVisualizer.DrawDocument(Graphics g)
 {
     if (HasDocument)
     {
         IGraphics graphics = new GdiGraphics(g);
         Document.Print(graphics, false, Style.CurrentStyle);
     }
 }
예제 #2
0
        /// <exception cref="ArgumentNullException">
        /// <paramref name="document"/> is null.
        /// </exception>
        public static void CopyAsImage(IPrintable document, bool selectedOnly)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            RectangleF areaF = document.GetPrintingArea(true);

            areaF.Offset(0.5F, 0.5F);
            Rectangle area = Rectangle.FromLTRB((int)areaF.Left, (int)areaF.Top,
                                                (int)Math.Ceiling(areaF.Right), (int)Math.Ceiling(areaF.Bottom));

            using (Bitmap image = new Bitmap(area.Width, area.Height, PixelFormat.Format24bppRgb))
                using (Graphics g = Graphics.FromImage(image))
                {
                    // Set drawing parameters
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    if (DiagramEditor.Settings.Default.UseClearTypeForImages)
                    {
                        g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                    }
                    else
                    {
                        g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
                    }
                    g.TranslateTransform(-area.Left, -area.Top);

                    // Draw image
                    g.Clear(Style.CurrentStyle.BackgroundColor);
                    IGraphics graphics = new GdiGraphics(g);
                    document.Print(graphics, selectedOnly, Style.CurrentStyle);

                    try
                    {
                        System.Windows.Forms.Clipboard.SetImage(image);
                    }
                    catch
                    {
                        //UNDONE: exception handling of CopyAsImage()
                    }
                }
        }
예제 #3
0
        private static void SaveAsImage(IPrintable document, string path,
                                        ImageFormat format, bool selectedOnly, bool transparent)
        {
            const int Margin = 20;

            RectangleF areaF = document.GetPrintingArea(selectedOnly);

            areaF.Offset(0.5F, 0.5F);
            Rectangle area = Rectangle.FromLTRB((int)areaF.Left, (int)areaF.Top,
                                                (int)Math.Ceiling(areaF.Right), (int)Math.Ceiling(areaF.Bottom));

            if (format == ImageFormat.Emf)             // Save to metafile
            {
                Graphics metaG = control.CreateGraphics();
                IntPtr   hc    = metaG.GetHdc();
                Graphics g     = null;

                try
                {
                    // Set drawing parameters
                    Metafile meta = new Metafile(path, hc);
                    g = Graphics.FromImage(meta);
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    if (DiagramEditor.Settings.Default.UseClearTypeForImages)
                    {
                        g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                    }
                    else
                    {
                        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                    }
                    g.TranslateTransform(-area.Left, -area.Top);

                    // Draw image
                    IGraphics graphics = new GdiGraphics(g);
                    document.Print(graphics, selectedOnly, Style.CurrentStyle);

                    meta.Dispose();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"{Strings.ErrorInSavingImage} {Strings.ErrorsReason} {ex.GetType()} {ex.Message}");
                }
                finally
                {
                    metaG.ReleaseHdc();
                    metaG.Dispose();
                    if (g != null)
                    {
                        g.Dispose();
                    }
                }
            }
            else             // Save to rastered image
            {
                int         width  = area.Width + Margin * 2;
                int         height = area.Height + Margin * 2;
                PixelFormat pixelFormat;

                if (transparent)
                {
                    pixelFormat = PixelFormat.Format32bppArgb;
                }
                else
                {
                    pixelFormat = PixelFormat.Format24bppRgb;
                }

                using (Bitmap image = new Bitmap(width, height, pixelFormat))
                    using (Graphics g = Graphics.FromImage(image))
                    {
                        // Set drawing parameters
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        if (DiagramEditor.Settings.Default.UseClearTypeForImages && !transparent)
                        {
                            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                        }
                        else
                        {
                            g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
                        }
                        g.TranslateTransform(Margin - area.Left, Margin - area.Top);

                        // Draw image
                        if (!transparent)
                        {
                            g.Clear(Style.CurrentStyle.BackgroundColor);
                        }

                        IGraphics graphics = new GdiGraphics(g);
                        document.Print(graphics, selectedOnly, Style.CurrentStyle);

                        try
                        {
                            image.Save(path, format);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine($"{Strings.ErrorInSavingImage} {Strings.ErrorsReason} {ex.GetType()} {ex.Message}");
                        }
                    }
            }
        }