예제 #1
0
        public static void PrintWithDPI(IGraphAreaBase ga, string description, double dpi, int margin = 0)
        {
            var visual = ga as Canvas;
            var pd     = new PrintDialog();

            if (pd.ShowDialog() == true)
            {
                ga.SetPrintMode(true, true, margin);
                //store original scale
                var originalScale = visual.LayoutTransform;
                //get scale from DPI
                var scale = dpi / DEFAULT_DPI;
                //Transform the Visual to scale
                var group = new TransformGroup();
                group.Children.Add(new ScaleTransform(scale, scale));
                visual.LayoutTransform = group;
                //update visual
                visual.InvalidateArrange();
                visual.UpdateLayout();

                //now print the visual to printer to fit on the one page.
                pd.PrintVisual(visual, description);
                //apply the original transform.
                visual.LayoutTransform = originalScale;
                ga.SetPrintMode(false, true, margin);
            }
        }
예제 #2
0
        public static void PrintToFit(IGraphAreaBase ga, string description, int margin = 0)
        {
            var visual = ga as Canvas;
            var pd     = new PrintDialog();

            if (pd.ShowDialog() == true)
            {
                ga.SetPrintMode(true, true, margin);

                //store original scale
                var originalScale = visual.LayoutTransform;
                //get selected printer capabilities
                var capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);

                //get scale of the print wrt to screen of WPF visual
                var scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / visual.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                                     visual.ActualHeight);

                //Transform the Visual to scale
                var group = new TransformGroup();
                group.Children.Add(new ScaleTransform(scale, scale));
                visual.LayoutTransform = group;
                visual.InvalidateArrange();
                visual.UpdateLayout();

                //now print the visual to printer to fit on the one page.
                pd.PrintVisual(visual, description);

                //apply the original transform.
                visual.LayoutTransform = originalScale;
                ga.SetPrintMode(false, true, margin);
            }
        }
예제 #3
0
        /// <summary>
        /// Method exports the GraphArea to an png image.
        /// </summary>
        /// <param name="surface">GraphArea control</param>
        /// <param name="path">Image destination path</param>
        /// <param name="useZoomControlSurface"></param>
        /// <param name="imgdpi">Optional image DPI parameter</param>
        /// <param name="imgQuality">Optional image quality parameter (for some formats like JPEG)</param>
        /// <param name="itype"></param>
        public static void ExportToImage(IGraphAreaBase surface, Uri path, ImageType itype, bool useZoomControlSurface = false, double imgdpi = DEFAULT_DPI, int imgQuality = 100)
        {
            if (!useZoomControlSurface)
            {
                surface.SetPrintMode(true, true, 100);
            }
            //Create a render bitmap and push the surface to it
            var vis = surface as UIElement;

            if (useZoomControlSurface)
            {
                var canvas      = (surface as Canvas);
                var zoomControl = canvas.Parent as IZoomControl;
                if (zoomControl != null)
                {
                    vis = zoomControl.PresenterVisual;
                }
                else
                {
                    var frameworkElement = canvas.Parent as FrameworkElement;
                    if (frameworkElement?.Parent is IZoomControl)
                    {
                        vis = ((IZoomControl)frameworkElement.Parent).PresenterVisual;
                    }
                }
            }

            var renderBitmap =
                new RenderTargetBitmap(
                    (int)(vis.DesiredSize.Width * (imgdpi / DEFAULT_DPI) + 100),
                    (int)(vis.DesiredSize.Height * (imgdpi / DEFAULT_DPI) + 100),
                    imgdpi,
                    imgdpi,
                    PixelFormat);

            //Render the graphlayout onto the bitmap.
            renderBitmap.Render(vis);


            //Create a file stream for saving image
            using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
            {
                //Use png encoder for our data
                BitmapEncoder encoder;
                switch (itype)
                {
                case ImageType.PNG: encoder = new PngBitmapEncoder();
                    break;

                case ImageType.JPEG: encoder = new JpegBitmapEncoder()
                {
                        QualityLevel = imgQuality
                };
                    break;

                case ImageType.BMP: encoder = new BmpBitmapEncoder();
                    break;

                case ImageType.GIF: encoder = new GifBitmapEncoder();
                    break;

                case ImageType.TIFF: encoder = new TiffBitmapEncoder();
                    break;

                default: throw new GX_InvalidDataException("ExportToImage() -> Unknown output image format specified!");
                }

                //Push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                //Save the data to the stream
                encoder.Save(outStream);
            }
            renderBitmap.Clear();
            renderBitmap = null;
            //due to mem leak in wpf :(
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            if (!useZoomControlSurface)
            {
                surface.SetPrintMode(false, true, 100);
            }
        }