Exemplo n.º 1
0
        /// <summary>
        /// Exports a map to a byte array
        /// </summary>
        /// <param name="mapView">the map view containing the content</param>
        /// <param name="sz">the size of the output image</param>
        /// <param name="useScaling">true, if the contend should be scaled to fit the image size:
        /// false, if the map scale shouldn't be changed.</param>
        /// <returns>The (jpg) image as byte array</returns>
        public byte[] ExportMap(MapView mapView, Size sz, bool useScaling)
        {
            Transform oldTransform = null;

            if (useScaling)
            {
                // Set the transform object for scaling.
                double scale = Math.Min(sz.Width / mapView.ActualWidth, sz.Height / mapView.ActualHeight);
                oldTransform            = mapView.LayoutTransform;
                mapView.LayoutTransform = new ScaleTransform(scale, scale);
            }

            // Set the size.
            var oldSize = new Size(mapView.ActualWidth, mapView.ActualHeight);

            mapView.Measure(sz);
            mapView.Arrange(new Rect(new Point(0, 0), sz));

            // Print.
            mapView.Printing = true;
            mapView.UpdateLayout();

            var renderTargetBitmap = new RenderTargetBitmap((int)sz.Width, (int)sz.Height, 96d, 96d, PixelFormats.Default);

            renderTargetBitmap.Render(mapView);

            mapView.Printing = false;

            // Reset the old values.
            if (useScaling)
            {
                mapView.LayoutTransform = oldTransform;
            }
            mapView.Measure(oldSize);
            mapView.Arrange(new Rect(new Point(0, 0), oldSize));

            // encode image
            var jpegBitmapEncoder = new JpegBitmapEncoder();

            jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

            var ms = new MemoryStream();

            jpegBitmapEncoder.Save(ms);
            ms.Close();
            return(ms.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Default print method of the map control,
        /// Exports a map to a byte array
        /// </summary>
        /// <param name="mapView">the map view containing the content</param>
        /// <param name="useScaling">true, if the contend should be scaled to fit the image size:
        /// false, if the map scale shouldn't be changed.</param>
        /// <param name="description"></param>
        public void PrintMap(MapView mapView, bool useScaling, string description)
        {
            var printDialog = new PrintDialog();

            if (printDialog.ShowDialog() == false)
            {
                return;
            }

            // Initialize variables.
            var       printCapabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
            Transform oldTransform      = null;

            if (useScaling)
            {
                // Set the transform object for scaling.
                double scale = Math.Min(printCapabilities.PageImageableArea.ExtentWidth / mapView.ActualWidth,
                                        printCapabilities.PageImageableArea.ExtentHeight / mapView.ActualHeight);
                oldTransform            = mapView.LayoutTransform;
                mapView.LayoutTransform = new ScaleTransform(scale, scale);
            }

            // Set the size.
            var oldSize = new Size(mapView.ActualWidth, mapView.ActualHeight);
            var size    = new Size(printCapabilities.PageImageableArea.ExtentWidth, printCapabilities.PageImageableArea.ExtentHeight);

            mapView.Measure(size);
            mapView.Arrange(new Rect(new Point(printCapabilities.PageImageableArea.OriginWidth, printCapabilities.PageImageableArea.OriginHeight), size));

            // Print.
            mapView.Printing = true;
            mapView.UpdateLayout();
            printDialog.PrintVisual(mapView, description);
            mapView.Printing = false;

            // Reset the old values.
            if (useScaling)
            {
                mapView.LayoutTransform = oldTransform;
            }
            mapView.Measure(oldSize);
            mapView.Arrange(new Rect(new Point(0, 0), oldSize));
        }