示例#1
0
        private void btnToBitmap_Click(object sender, RoutedEventArgs e)
        {
            PrinterInteractiveOverlay printerInteractiveOverlay = (PrinterInteractiveOverlay)Map1.InteractiveOverlays["PrintPreviewOverlay"];
            PagePrinterLayer          pagePrinterLayer          = printerInteractiveOverlay.PrinterLayers["PageLayer"] as PagePrinterLayer;

            // Create a bitmap that is the size of the pages bounding box.  The page by default is in points unit
            // system which is very similar to pixels so that ampping is nice.
            GeoImage bitmap = null;

            try
            {
                bitmap = new GeoImage((int)pagePrinterLayer.GetBoundingBox().Width, (int)pagePrinterLayer.GetBoundingBox().Height);

                // Create a GeoCanvas and start the drawing
                GeoCanvas gdiPlusGeoCanvas = GeoCanvas.CreateDefaultGeoCanvas();
                gdiPlusGeoCanvas.BeginDrawing(bitmap, pagePrinterLayer.GetBoundingBox(), Map1.MapUnit);

                // Loop through all of the PrintingLayer in the PrinterInteractiveOverlay and print all of the
                // except for the PagePrinterLayer
                Collection <SimpleCandidate> labelsInAllLayers = new Collection <SimpleCandidate>();
                foreach (PrinterLayer printerLayer in printerInteractiveOverlay.PrinterLayers)
                {
                    printerLayer.IsDrawing = true;
                    if (!(printerLayer is PagePrinterLayer))
                    {
                        printerLayer.Draw(gdiPlusGeoCanvas, labelsInAllLayers);
                    }
                    printerLayer.IsDrawing = false;
                }

                // End the drawing
                gdiPlusGeoCanvas.EndDrawing();

                //Save the resulting bitmap to a file and open the file to show the user
                string filename = "PrintingResults.bmp";
                bitmap.Save(filename);
                Process.Start("explorer.exe", filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.StackTrace, "Exception");
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Dispose();
                }
            }
        }
        protected override void DrawCore(IEnumerable <Feature> features, GeoCanvas canvas, System.Collections.ObjectModel.Collection <SimpleCandidate> labelsInThisLayer, System.Collections.ObjectModel.Collection <SimpleCandidate> labelsInAllLayers)
        {
            Bitmap       intensityBitmap = null;
            Bitmap       colorBitmap     = null;
            GeoImage     geoImage        = null;
            MemoryStream pngStream       = null;

            try
            {
                // Load the default color palette if it does not exisit
                // or does not have the right amount of colors
                if (colorPalette.Count != 256)
                {
                    colorPalette = GetDefaultColorPalette();
                }

                intensityBitmap = new Bitmap(Convert.ToInt32(canvas.Width), Convert.ToInt32(canvas.Height));
                Graphics Surface = Graphics.FromImage(intensityBitmap);
                Surface.Clear(Color.Transparent);
                Surface.Dispose();

                List <HeatPoint> heatPoints = new List <HeatPoint>();

                foreach (Feature feature in features)
                {
                    if (feature.GetWellKnownType() == WellKnownType.Point)
                    {
                        PointShape   pointShape  = (PointShape)feature.GetShape();
                        ScreenPointF screenPoint = ExtentHelper.ToScreenCoordinate(canvas.CurrentWorldExtent, pointShape, canvas.Width, canvas.Height);

                        double realValue;
                        if (intensityRangeStart != 0 && intensityRangeEnd != 0 && intensityRangeStart != intensityRangeEnd && intensityColumnName != string.Empty)
                        {
                            if (intensityRangeStart < intensityRangeEnd)
                            {
                                realValue = (255 / (intensityRangeEnd - intensityRangeStart)) * (GetDoubleValue(feature.ColumnValues[intensityColumnName], intensityRangeStart, intensityRangeEnd) - intensityRangeStart);
                            }
                            else
                            {
                                realValue = (255 / (intensityRangeEnd - intensityRangeStart)) * (intensityRangeEnd - GetDoubleValue(feature.ColumnValues[intensityColumnName], intensityRangeStart, intensityRangeEnd));
                            }
                        }
                        else
                        {
                            realValue = intensity;
                        }

                        HeatPoint heatPoint = new HeatPoint(Convert.ToInt32(screenPoint.X), Convert.ToInt32(screenPoint.Y), Convert.ToByte(realValue));
                        heatPoints.Add(heatPoint);
                    }
                }


                //Calls the function to get the pixel size of the point based on the world point size and the current extent of the map.
                float size = GetPointSize(canvas);


                // Create the intensity bitmap
                intensityBitmap = CreateIntensityMask(intensityBitmap, heatPoints, Convert.ToInt32(size));

                // Color the intensity bitmap
                colorBitmap = Colorize(intensityBitmap, (byte)alpha, colorPalette);

                // Create a geoimage from the color bitmap
                pngStream = new MemoryStream();
                colorBitmap.Save(pngStream, System.Drawing.Imaging.ImageFormat.Png);
                geoImage = new GeoImage(pngStream);

                // Write the geoimage to the canvas
                canvas.DrawWorldImageWithoutScaling(geoImage, canvas.CurrentWorldExtent.GetCenterPoint().X, canvas.CurrentWorldExtent.GetCenterPoint().Y, DrawingLevel.LevelOne);
            }
            finally
            {
                // Make sure we clean up all the memeory that we use durring the process
                if (intensityBitmap != null)
                {
                    intensityBitmap.Dispose();
                }
                if (colorBitmap != null)
                {
                    colorBitmap.Dispose();
                }
                if (geoImage != null)
                {
                    geoImage.Dispose();
                }
                if (pngStream != null)
                {
                    pngStream.Dispose();
                }
            }
        }