ThinkGeo.com    |     Documentation    |     Premium Support

How to get screenshot of the map into Bitmap object?

I am using WinformsMap object, I'd like to get a screenshot from the map as Bitmap object. Is this possible?



Hello 
  
  Rectangle rDrawingMap = new Rectangle(0, 0, Map.Width, Map.Height); 
  using (Bitmap bitmap = new Bitmap(rDrawingMap.Width, rDrawingMap.Height)) 
 { 
   Map.DrawToBitmap(bitmap, rDrawingMap); 
 } 
  
 Patrick.

Pawel & Patrick, 
  
 Thanks for your posts and sharing! It is exactly with what Patrick suggested! 
  
 Any more questions just let me know. 
  
 Thanks. 
  
 Yale

Can this method also be used in the WPF version? I don’t see it in the WPF map control…

Gregory,


This method cannot be sued in WPF version because their base classes are different. Following codes shows you how to save the current MapView into a bitmap.

 



private static PngBitmapEncoder getImageFromControl(Canvas controlToConvert)
        {
            // save current canvas transform
            Transform transform = controlToConvert.LayoutTransform;

            // get size of control
            System.Windows.Size sizeOfControl = new System.Windows.Size(controlToConvert.ActualWidth, controlToConvert.ActualHeight);
            // measure and arrange the control
            controlToConvert.Measure(sizeOfControl);
            // arrange the surface
            controlToConvert.Arrange(new Rect(sizeOfControl));

            // craete and render surface and push bitmap to it
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)sizeOfControl.Width, (int)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
            // now render surface to bitmap
            renderBitmap.Render(controlToConvert);

            // encode png data
            PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
            // puch rendered bitmap into it
            pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

            // return encoder
            return pngEncoder;
        }


        public static ImageSource GetImageOfControl(Canvas controlToConvert)
        {
            // return first frame of image 
            return getImageFromControl(controlToConvert).Frames[0];
        }


        public static bool SaveImageOfControl(Canvas controlToConvert, string fileName)
        {
            try
            {
                // create a file stream for saving image
                using (System.IO.FileStream outStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
                {
                    // save encoded data to stream
                    getImageFromControl(controlToConvert).Save(outStream);
                }
            }
            catch (Exception e)
            {
                // display for debugging
                Console.WriteLine("Exception caught saving stream: {0}", e.Message);
                // return fail
                return false;
            }

            // return that passed
            return true;
        }

Any more questions please feel free to let me know.


 
Thanks.
 
Yale