Inheritance: IGraphics
Example #1
0
 void IDocumentVisualizer.DrawDocument(Graphics g)
 {
     if (HasDocument)
     {
         IGraphics graphics = new GdiGraphics(g);
         Document.Print(graphics, false, Style.CurrentStyle);
     }
 }
Example #2
0
 public void DrawDocument(Graphics g)
 {
     if (HasDocument)
     {
         IGraphics graphics = new GdiGraphics(g);
         Document.Print(graphics, false, Style.CurrentStyle);
     }
 }
Example #3
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (HasDocument)
            {
                var graphics = new GdiGraphics(e.Graphics);
                DrawContent(graphics);
            }
        }
Example #4
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");
            }

            var areaF = document.GetPrintingArea(true);

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

            using (var image = new Bitmap(area.Width, area.Height, PixelFormat.Format24bppRgb))
                using (var g = Graphics.FromImage(image))
                {
                    // Set drawing parameters
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    if (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()
                    }
                }
        }
Example #5
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()
				}
			}
		}
Example #6
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)
                {
                    MessageBox.Show(
                        string.Format("{0}\n{1}: {2}", Strings.ErrorInSavingImage,
                                      Strings.ErrorsReason, ex.Message),
                        Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                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)
                        {
                            MessageBox.Show(
                                string.Format("{0}\n{1}: {2}", Strings.ErrorInSavingImage,
                                              Strings.ErrorsReason, ex.Message),
                                Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
            }
        }
Example #7
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)
				{
					MessageBox.Show(
						string.Format("{0}\n{1}: {2}", Strings.ErrorInSavingImage,
							Strings.ErrorsReason, ex.Message),
						Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				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)
					{
						MessageBox.Show(
							string.Format("{0}\n{1}: {2}", Strings.ErrorInSavingImage,
								Strings.ErrorsReason, ex.Message),
							Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}
Example #8
0
		void IDocumentVisualizer.DrawDocument(Graphics g)
		{
			if (HasDocument)
			{
				IGraphics graphics = new GdiGraphics(g);
				Document.Print(graphics, false, Style.CurrentStyle);
			}
		}
Example #9
0
		private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
		{
			// Scale the page to match sizes of the screen
			e.Graphics.PageUnit = GraphicsUnit.Inch;
			e.Graphics.PageScale = 1 / DiagramElement.Graphics.DpiX;

			// Get the phisical page margins
			float marginScale = DiagramElement.Graphics.DpiX / 100;
			RectangleF marginBounds = e.MarginBounds;
			if (!printDocument.PrintController.IsPreview)
				marginBounds.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY);
			marginBounds = new RectangleF(
				marginBounds.X * marginScale, marginBounds.Y * marginScale,
				marginBounds.Width * marginScale, marginBounds.Height * marginScale);

			// Get logical area information
			RectangleF drawingArea = document.GetPrintingArea(selectedOnly);
			int column = pageIndex % columns;
			int row = pageIndex / columns;

			// Get zooming information if diagram is too big
			float scaleX = columns * marginBounds.Width / drawingArea.Width;
			float scaleY = rows * marginBounds.Height / drawingArea.Height;
			float scale = Math.Min(scaleX, scaleY);
			if (scale > 1) scale = 1; // No need for zooming in

			// Set the printing clip region
			RectangleF clipBounds = marginBounds;
			if (column == 0)
			{
				clipBounds.X = 0;
				clipBounds.Width += marginBounds.Left;
			}
			if (row == 0)
			{
				clipBounds.Y = 0;
				clipBounds.Height += marginBounds.Top;
			}
			if (column == columns - 1)
			{
				clipBounds.Width += marginBounds.Left;
			}
			if (row == rows - 1)
			{
				clipBounds.Height += marginBounds.Top;
			}
			e.Graphics.SetClip(clipBounds);

			// Moving the image to it's right position
			e.Graphics.TranslateTransform(-column * marginBounds.Width, -row * marginBounds.Height);
			e.Graphics.TranslateTransform(marginBounds.Left, marginBounds.Top);
			e.Graphics.ScaleTransform(scale, scale);
			e.Graphics.TranslateTransform(-drawingArea.Left, -drawingArea.Top);
			
			// Printing
			IGraphics graphics = new GdiGraphics(e.Graphics);
			document.Print(graphics, selectedOnly, printingStyle);
			e.HasMorePages = (++pageIndex < PageCount);
		}