Question

I'm trying to capture a screenshot of the MainWindow and content on the main window. The MainWindow will sometimes be behind other elements and need to get screen capture without those other overlaying elements. This code currently returns a bitmap of the blank MainWindow form without any content. The form has a bunch of different dynamic UI elements. How can I take a current screenshot/capture of MainWindow and it's content? Code for the MainWindow content is waay to long to post so I hope that this is enough.

Rectangle bounds = new Rectangle();
bounds.Width = Program.MainWindow.Width;
bounds.Height = Program.MainWindow.Height;

screenShot = new Bitmap(Program.MainWindow.Width,
                       Program.MainWindow.Height,
                       PixelFormat.Format32bppRgb);
Program.MainWindow.DrawToBitmap(screenShot, bounds);
Was it helpful?

Solution

You should be able to get it by using the CopyFromScreen method of the Graphics class

        Rectangle bounds = Program.MainWindow.Bounds;
        Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
        }
        b.Save("YOUR FILE NAME HERE");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top