Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

c# - Screen Capture Not Capturing Dialog Boxes in My application

I have an application A that is made in WPF and WinForms.I have written another Application in WinForms for Capturing Screen. The problem I'm facing is that The dialog boxes that come up in Application A do not captured in the screen. The whole screen gets captured including the area behind the dialog box but the dialog box doesn't get captured.

    public void CaptureScreen(string filepath)
    {

        string[] words = filepath.Split('\');
        string newFilePath = " ";
        foreach (string word in words)
        {
            if (!(word.Contains(".bmp")))
            {
                newFilePath = newFilePath + word + "//";
            }
            else
            {
                newFilePath = newFilePath + word;
            }


        }

        this.WindowState = FormWindowState.Minimized;


        Screen[] screens;
        screens = Screen.AllScreens;
        int noofscreens = screens.Length, maxwidth = 0, maxheight = 0;
        for (int i = 0; i < noofscreens; i++)
        {
            if (maxwidth < (screens[i].Bounds.X + screens[i].Bounds.Width)) maxwidth = screens[i].Bounds.X + screens[i].Bounds.Width;
            if (maxheight < (screens[i].Bounds.Y + screens[i].Bounds.Height)) maxheight = screens[i].Bounds.Y + screens[i].Bounds.Height;
        }


        var width = maxwidth;
        var height = maxheight;

        Point sourcePoint = Point.Empty;
        Point destinationPoint = Point.Empty;

        Rectangle rect = new Rectangle(0, 0, width, height);

        Bitmap bitmap = new Bitmap(rect.Width, rect.Height);

        Graphics g = Graphics.FromImage(bitmap);

       // g.CopyFromScreen(sourcePoint, destinationPoint, rect.Size);

        g.CopyFromScreen(new Point(rect.Left, rect.Top), Point.Empty, rect.Size);

        bitmap.Save(filepath, ImageFormat.Bmp);


        //Console.WriteLine("(width, height) = ({0}, {1})", maxx - minx, maxy - miny);


    }


}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 Bitmap bmpScreenshot = new Bitmap(Screen.AllScreens[1].Bounds.Width, Screen.AllScreens[1].Bounds.Height, PixelFormat.Format32bppArgb);

            Graphics.FromImage(bmpScreenshot).CopyFromScreen(
                                         Screen.AllScreens[1].Bounds.X,
                                         Screen.AllScreens[1].Bounds.Y,
                                         0,
                                         0,
                                         Screen.AllScreens[1].Bounds.Size,
                                         CopyPixelOperation.SourceCopy);

            this.picExtendedModitorScreen.Image = bmpScreenshot;
            this.picExtendedModitorScreen.Refresh();

Put this code in timer tick event.

I have put extended screen 1 in the code you can change it to any other.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...