SilverlightMappingToolBasic.UI.SuperGraph.View.SuperGraphControl.GetMapBounds C# (CSharp) Method

GetMapBounds() public method

public GetMapBounds ( double &left, double &right, double &top, double &bottom ) : void
left double
right double
top double
bottom double
return void
        public void GetMapBounds(out double left, out double right, out double top, out double bottom)
        {
            var visibleNodes = GetVisibleNodeControls();

            var locationList = visibleNodes.Select(q => q.Centre).ToList();

            left = locationList.Min(q => q.X);
            right = locationList.Max(q => q.X);
            top = locationList.Min(q => q.Y);
            bottom = locationList.Max(q => q.Y);
        }

Usage Example

        public void SaveToImage(SuperGraphControl mapCanvas) 
        {
            
            var saveDlg = new SaveFileDialog {Filter = "JPEG Files (*.jpg)|*.jpg", DefaultExt = ".jpg"};

            var showDialog = saveDlg.ShowDialog();
            if (showDialog != null && (bool)showDialog)
            {
                WriteableBitmap bitmap;
                try
                {
                    
                    double left, right, top, bottom;
                    mapCanvas.GetMapBounds(out left, out right, out top, out bottom);
                    left -= 70;
                    right += 70;
                    top -= 170;
                    bottom += 170;
                    double mapHeight = bottom - top;
                    double mapWidth = right - left;

                    var transform = new TransformGroup();
                    ScaleTransform st;
                    TranslateTransform tt;
                    
                    var moved = mapCanvas.MoveGraphTransform;
                    double xMove, yMove;

                    const ScreenCaptureType type = ScreenCaptureType.ZoomedFullScale;
                    Canvas canvas;
                    switch (type)
                    {
                        case ScreenCaptureType.FullScale:

                            xMove = moved.X + left;
                            yMove = moved.Y + top;

                            st = new ScaleTransform
                            {
                                ScaleX = 1 / mapCanvas.Zoom,
                                ScaleY = 1 / mapCanvas.Zoom,
                                CenterX = 0,
                                CenterY = 0
                            };
                            transform.Children.Add(st);


                            tt = new TranslateTransform
                            {
                                X = -xMove,
                                Y = -yMove,
                            };
                            transform.Children.Add(tt);
                            canvas = new Canvas
                            {
                                Background = new SolidColorBrush(Colors.White),
                                Width = (int) mapWidth,
                                Height = (int) mapHeight
                            };
                            bitmap = new WriteableBitmap(canvas, null);
                            break;
                        case ScreenCaptureType.ZoomedFullScale:
                            xMove = moved.X + left;
                            yMove = moved.Y + top;

                            tt = new TranslateTransform
                            {
                                X = -xMove * mapCanvas.Zoom,
                                Y = -yMove * mapCanvas.Zoom,
                            };
                            transform.Children.Add(tt);
                            canvas = new Canvas
                            {
                                Background = new SolidColorBrush(Colors.White),
                                Width = (int) (mapWidth*mapCanvas.Zoom),
                                Height = (int) (mapHeight*mapCanvas.Zoom)
                            };
                            bitmap = new WriteableBitmap(canvas, null);
                            break;
                        case ScreenCaptureType.CurrentScreenOnly:
                            canvas = new Canvas
                            {
                                Background = new SolidColorBrush(Colors.White),
                                Width = (int) mapCanvas.ActualWidth,
                                Height = (int) mapCanvas.ActualHeight
                            };
                            bitmap = new WriteableBitmap(canvas, null);
                            break;
                        default:
                            canvas = new Canvas
                            {
                                Background = new SolidColorBrush(Colors.White),
                                Width = (int) mapCanvas.ActualWidth,
                                Height = (int) mapCanvas.ActualHeight
                            };
                            bitmap = new WriteableBitmap(canvas, null);
                            break;
                    }

                    bitmap.Render(mapCanvas, transform);
                    bitmap.Invalidate();


                    using (var fs = saveDlg.OpenFile())
                    {
                        var stream = GetImageStream(bitmap);

                        //Get Bytes from memory stream and write into IO stream
                        var binaryData = new Byte[stream.Length];
                        var bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
                        fs.Write(binaryData, 0, binaryData.Length);
                    }

                    

                }
                catch (Exception e)
                {
                    SuperMessageBoxService.ShowError("Export Failed", "We are unable to export your map.\r\nPlease zoom out and try again.");
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        
        }
SuperGraphControl