vendredi 8 mai 2015

Take a screenshot of a WPF window with the predefined image size without losing quality

When I take a screenshot of a current WPF window, the image resolution is that of my monitor (if the app is maximized), which is ok. However, if I was to print that image to a much bigger format, the image would look blurry. I found the way to capture the current window, and save it as a png file, but it's not doing the trick. The image is saved with the resolution I set, but the actual wpf window takes only a small portion of the saved image. Example is taken from:

http://ift.tt/1wybIDL

        var screen = System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

        var rtb = new RenderTargetBitmap(4000, 4000, 96, 96, PixelFormats.Pbgra32);

        rtb.Render(screen);

        var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
        enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));

        using (var stm = System.IO.File.Create("ScreenShot.png"))
        {
            enc.Save(stm);
            using (Image img = Image.FromStream(stm))
            {
                Rectangle dest = new Rectangle(0, 0, 6000, 4000);

                using (Graphics imgG = Graphics.FromImage(img))
                {
                    imgG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    imgG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    imgG.DrawImage(img, dest);
                }

                img.Save("NewScreenShot.png");
            }
        }

So basically, I'd like to capture the screenshot with the resolution of 4000 x 4000, if that's possible, without losing quality. The above code produces an image of 4000 x 4000, however the screenshot only takes a small portion of it, its original resolution.

Aucun commentaire:

Enregistrer un commentaire