Пример #1
0
        /// <summary>
        /// Stores the current contents of the back buffer as a .PNG file.
        /// </summary>
        /// <param name="inGraphicsDevice">GraphicsDevice to grab the back buffer data from.</param>
        /// <param name="inFilename">String containing the name of the file to save.</param>
        /// <returns>True if the file is saved successfully, otherwise false.</returns>
        public static bool Capture(GraphicsDevice inGraphicsDevice,
                                   String inFilename, ScreenCaptureType inCaptureExtension)
        {
            if (inCaptureExtension == ScreenCaptureType.UseJPEG) { inFilename = inFilename + ".jpg"; }
            if (inCaptureExtension == ScreenCaptureType.UsePNG) { inFilename = inFilename + ".png"; }

            // Store the current BackBuffer data in a new array of Color values. This
            // will take what is currently on the BackBuffer; everything current being
            // drawn to the screen.
            Color[] colorData = new Color[inGraphicsDevice.Viewport.Width *
                                          inGraphicsDevice.Viewport.Height];
            inGraphicsDevice.GetBackBufferData<Color>(colorData);

            // Next set the colors into a Texture, ready for saving.
            Texture2D backBufferTexture = new Texture2D(inGraphicsDevice,
                                                        inGraphicsDevice.Viewport.Width,
                                                        inGraphicsDevice.Viewport.Height);
            backBufferTexture.SetData<Color>(colorData, 0, colorData.Length);

            // Create the file after checking whether it exists. This requires a means
            // of altering the intended filename so that it cannot overwrite an existing
            // screen-capture, for instance suffixing an incremental digit onto the file
            // name, but this would have to be saved to avoid overwritten files when the
            // game crashes and the count is lost.
            if (!File.Exists(inFilename))
            {
                using (FileStream fileStream = File.Create(inFilename))
                {
                    // The choice passed in as the 3rd parameter just exists for the sake
                    // of providing options. If one is clearly advantageous to the other
                    // I'll hard-code it to use that instead. But, for now, we allow the
                    // choice between JPEG and PNG. PNG files have transparency.
                    switch (inCaptureExtension)
                    {
                        case ScreenCaptureType.UseJPEG:
                            backBufferTexture.SaveAsJpeg(fileStream,
                                                         inGraphicsDevice.Viewport.Width,
                                                         inGraphicsDevice.Viewport.Height);
                            break;
                        case ScreenCaptureType.UsePNG:
                            backBufferTexture.SaveAsPng(fileStream,
                                                        inGraphicsDevice.Viewport.Width,
                                                        inGraphicsDevice.Viewport.Height);
                            break;
                    }

                    fileStream.Flush();
                }

                return true;
            }

            return false;
        }
Пример #2
0
        private bool HotkeyCaptureScreenshot()
        {
            if (ModelPanel.Focused)
            {
                SaveBitmap(ModelPanel.GetScreenshot(ModelPanel.ClientRectangle, false), ScreenCaptureFolder,
                           "." + ScreenCaptureType.ToString());
                return(true);
            }

            return(false);
        }
Пример #3
0
        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();
            }
        }