문제

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);
도움이 되었습니까?

해결책

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");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top