Пример #1
0
        /// <summary>
        /// Renders the graph document as enhanced metafile in vector format.
        /// </summary>
        /// <param name="doc">graph document.</param>
        /// <param name="stream">The stream the metafile is rendered to.</param>
        /// <param name="sourceDpiResolution">The resolution in dpi of the source.</param>
        /// <param name="outputScalingFactor">Output scaling factor. If less than 1, the image will appear smaller than originally, if greater than 1, the image will appear larger than originally.</param>
        /// <param name="backgroundBrush">The background brush. This argument can be null, or the brush can be transparent.</param>
        /// <param name="pixelFormat">Optional: Only used if the graphics context can not be created from a printer document. Pixel format of the bitmap that is used in this case to construct the graphics context.</param>
        public static (int pixelsX, int pixelsY) RenderAsEnhancedMetafileVectorFormatToStream(GraphDocument doc, System.IO.Stream stream, double sourceDpiResolution, double outputScalingFactor, BrushX backgroundBrush = null, PixelFormat pixelFormat = PixelFormat.Format32bppArgb)
        {
            var renderingProc = new Action <Graphics>(
                (grfxMetafile) =>
            {
                if (backgroundBrush != null)
                {
                    backgroundBrush.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
                    grfxMetafile.FillRectangle(backgroundBrush, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
                }

#if DIAGNOSTICLINERENDERING
                {
                    grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX * 2, fDocSizeY * 1.3f);

                    grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX, fDocSizeY);
                    grfxMetafile.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX, 0);
                    grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX / 4, fDocSizeY / 2);
                    grfxMetafile.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX / 4, fDocSizeY / 2);
                    grfxMetafile.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, 0);
                    grfxMetafile.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, fDocSizeY);
                }
#endif

                doc.Paint(grfxMetafile, true);
            });

            return(RenderAsEnhancedMetafileToStream(renderingProc, stream, doc.Size, sourceDpiResolution, outputScalingFactor, pixelFormat));
        }
Пример #2
0
        /// <summary>
        /// Saves the graph as an bitmap file and returns the bitmap.
        /// </summary>
        /// <param name="doc">The graph document to export.</param>
        /// <param name="backbrush1">First brush used to fill the background of the image. Can be <c>null</c>.</param>
        /// <param name="backbrush2">Second brush used to fill the background of the image. Can be <c>null</c>.</param>
        /// <param name="pixelformat">Specify the pixelformat here.</param>
        /// <param name="sourceDpiResolution">Resolution at which the graph document is rendered into a bitmap.</param>
        /// <param name="destinationDpiResolution">Resolution which is assigned to the bitmap. This determines the physical size of the bitmap.</param>
        /// <returns>The saved bitmap. You should call Dispose when you no longer need the bitmap.</returns>
        public static Bitmap RenderAsBitmap(this GraphDocument doc, BrushX backbrush1, BrushX backbrush2, PixelFormat pixelformat, double sourceDpiResolution, double destinationDpiResolution)
        {
            // round the pixels to multiples of 4, many programs rely on this
            int bmpWidth  = (int)(4 * Math.Ceiling(0.25 * doc.Size.X * sourceDpiResolution / 72));
            int bmpHeight = (int)(4 * Math.Ceiling(0.25 * doc.Size.Y * sourceDpiResolution / 72));
            var bitmap    = new System.Drawing.Bitmap(bmpWidth, bmpHeight, pixelformat);

            double outputScaling = sourceDpiResolution / destinationDpiResolution;

            bitmap.SetResolution((float)(bmpWidth / (outputScaling * doc.Size.X / 72)), (float)(bmpHeight / (outputScaling * doc.Size.Y / 72)));

            using (var grfx = Graphics.FromImage(bitmap))
            {
                // Set everything to high quality
                grfx.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                grfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                // 2014-10-10 Setting InterpolationMode to HighQualityBicubic and PixelOffsetMode to HighQuality
                // causes problems when rendering small bitmaps (at a large magnification, for instance the density image legend):
                // the resulting image seems a litte soft, the colors somehow distorted, so I decided not to use them here any more

                //		grfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //		grfx.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                grfx.PageUnit = GraphicsUnit.Point;
                grfx.ScaleTransform((float)outputScaling, (float)outputScaling);
                grfx.SetClip(new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));

                if (null != backbrush1)
                {
                    backbrush1.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
                    grfx.FillRectangle(backbrush1, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
                }

                if (null != backbrush2)
                {
                    backbrush2.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
                    grfx.FillRectangle(backbrush2, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
                }

#if DIAGNOSTICLINERENDERING
                {
                    var fDocSizeX = (float)doc.Size.X;
                    var fDocSizeY = (float)doc.Size.Y;
                    grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX * 2, fDocSizeY * 1.3f);

                    grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX, fDocSizeY);
                    grfx.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX, 0);
                    grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX / 4, fDocSizeY / 2);
                    grfx.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX / 4, fDocSizeY / 2);
                    grfx.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, 0);
                    grfx.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, fDocSizeY);
                }
#endif

                doc.Paint(grfx, true);
            }

            bitmap.SetResolution((float)destinationDpiResolution, (float)destinationDpiResolution);

            return(bitmap);
        }
Пример #3
0
		/// <summary>
		/// Saves the graph as an bitmap file and returns the bitmap.
		/// </summary>
		/// <param name="doc">The graph document to export.</param>
		/// <param name="backbrush1">First brush used to fill the background of the image. Can be <c>null</c>.</param>
		/// <param name="backbrush2">Second brush used to fill the background of the image. Can be <c>null</c>.</param>
		/// <param name="pixelformat">Specify the pixelformat here.</param>
		/// <param name="sourceDpiResolution">Resolution at which the graph document is rendered into a bitmap.</param>
		/// <param name="destinationDpiResolution">Resolution which is assigned to the bitmap. This determines the physical size of the bitmap.</param>
		/// <returns>The saved bitmap. You should call Dispose when you no longer need the bitmap.</returns>
		public static Bitmap RenderAsBitmap(this GraphDocument doc, BrushX backbrush1, BrushX backbrush2, PixelFormat pixelformat, double sourceDpiResolution, double destinationDpiResolution)
		{
			// round the pixels to multiples of 4, many programs rely on this
			int bmpWidth = (int)(4 * Math.Ceiling(0.25 * doc.Size.X * sourceDpiResolution / 72));
			int bmpHeight = (int)(4 * Math.Ceiling(0.25 * doc.Size.Y * sourceDpiResolution / 72));
			System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bmpWidth, bmpHeight, pixelformat);

			double outputScaling = sourceDpiResolution / destinationDpiResolution;
			bitmap.SetResolution((float)(bmpWidth / (outputScaling * doc.Size.X / 72)), (float)(bmpHeight / (outputScaling * doc.Size.Y / 72)));

			using (Graphics grfx = Graphics.FromImage(bitmap))
			{
				// Set everything to high quality
				grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
				grfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

				// 2014-10-10 Setting InterpolationMode to HighQualityBicubic and PixelOffsetMode to HighQuality
				// causes problems when rendering small bitmaps (at a large magnification, for instance the density image legend):
				// the resulting image seems a litte soft, the colors somehow distorted, so I decided not to use them here any more

				//		grfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
				//		grfx.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

				grfx.PageUnit = GraphicsUnit.Point;
				grfx.ScaleTransform((float)outputScaling, (float)outputScaling);
				grfx.SetClip(new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));

				if (null != backbrush1)
				{
					backbrush1.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
					grfx.FillRectangle(backbrush1, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
				}

				if (null != backbrush2)
				{
					backbrush2.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
					grfx.FillRectangle(backbrush2, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
				}

#if DIAGNOSTICLINERENDERING
				{
					var fDocSizeX = (float)doc.Size.X;
					var fDocSizeY = (float)doc.Size.Y;
					grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX * 2, fDocSizeY * 1.3f);

					grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX, fDocSizeY);
					grfx.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX, 0);
					grfx.DrawLine(Pens.Black, 0, 0, fDocSizeX / 4, fDocSizeY / 2);
					grfx.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX / 4, fDocSizeY / 2);
					grfx.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, 0);
					grfx.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, fDocSizeY);
				}
#endif

				doc.Paint(grfx, true);
			}

			bitmap.SetResolution((float)destinationDpiResolution, (float)destinationDpiResolution);

			return bitmap;
		}
Пример #4
0
		/// <summary>
		/// Renders the graph document as enhanced metafile in vector format.
		/// </summary>
		/// <param name="doc">graph document.</param>
		/// <param name="stream">The stream the metafile is rendered to.</param>
		/// <param name="sourceDpiResolution">The resolution in dpi of the source.</param>
		/// <param name="outputScalingFactor">Output scaling factor. If less than 1, the image will appear smaller than originally, if greater than 1, the image will appear larger than originally.</param>
		/// <param name="backgroundBrush">The background brush. This argument can be null, or the brush can be transparent.</param>
		/// <param name="pixelFormat">Optional: Only used if the graphics context can not be created from a printer document. Pixel format of the bitmap that is used in this case to construct the graphics context.</param>
		public static void RenderAsEnhancedMetafileVectorFormatToStream(GraphDocument doc, System.IO.Stream stream, double sourceDpiResolution, double outputScalingFactor, BrushX backgroundBrush = null, PixelFormat pixelFormat = PixelFormat.Format32bppArgb)
		{
			var renderingProc = new Action<Graphics>(
					(grfxMetafile) =>
					{
						if (backgroundBrush != null)
						{
							backgroundBrush.SetEnvironment(new RectangleD2D(0, 0, doc.Size.X, doc.Size.Y), sourceDpiResolution);
							grfxMetafile.FillRectangle(backgroundBrush, new RectangleF(0, 0, (float)doc.Size.X, (float)doc.Size.Y));
						}

#if DIAGNOSTICLINERENDERING
				{
					grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX * 2, fDocSizeY * 1.3f);

					grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX, fDocSizeY);
					grfxMetafile.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX, 0);
					grfxMetafile.DrawLine(Pens.Black, 0, 0, fDocSizeX / 4, fDocSizeY / 2);
					grfxMetafile.DrawLine(Pens.Black, 0, fDocSizeY, fDocSizeX / 4, fDocSizeY / 2);
					grfxMetafile.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, 0);
					grfxMetafile.DrawLine(Pens.Black, fDocSizeX * 0.75f, fDocSizeY / 2, fDocSizeX, fDocSizeY);
				}
#endif

						doc.Paint(grfxMetafile, true);
					});

			RenderAsEnhancedMetafileToStream(renderingProc, stream, doc.Size, sourceDpiResolution, outputScalingFactor, pixelFormat);
		}
		public void UpdatePreview(BrushX brush)
		{
			if (null == _previewPanel || null == brush)
				return;

			int height = (int)_previewPanel.ActualHeight;
			int width = (int)_previewPanel.ActualWidth;
			if (height <= 0)
				height = 64;
			if (width <= 0)
				width = 64;

			if (null == _previewBitmap)
			{
				_previewBitmap = new GdiToWpfBitmap(width, height);
				_previewPanel.Source = _previewBitmap.WpfBitmap;
			}

			if (width != _previewBitmap.GdiRectangle.Width || height != _previewBitmap.GdiRectangle.Height)
			{
				_previewBitmap.Resize(width, height);
				_previewPanel.Source = _previewBitmap.WpfBitmap;
			}

			using (var grfx = _previewBitmap.BeginGdiPainting())
			{
				var fullRect = _previewBitmap.GdiRectangle;

				grfx.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
				grfx.FillRectangle(System.Drawing.Brushes.Transparent, fullRect);
				grfx.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;

				var r2 = fullRect;
				r2.Inflate(-r2.Width / 4, -r2.Height / 4);
				//grfx.FillRectangle(System.Drawing.Brushes.Black, r2);

				brush.SetEnvironment(fullRect, BrushX.GetEffectiveMaximumResolution(grfx));
				grfx.FillRectangle(brush, fullRect);

				_previewBitmap.EndGdiPainting();
			}
		}