首页 > 解决方案 > c# 2个线程同时使用graphics.CopyFromScreen

问题描述

我想同时做2个屏幕截图。我怎样才能做到这一点?当我创建了 2 个后台工作人员并且每个都包含 CopyFromScreen 方法时,它会抛出一个错误:

System.ArgumentException:参数开玩笑 nieprawidłowy。w System.Drawing.Graphics.GetHdc() w System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) w System.Drawing.Graphics.CopyFromScreen(Point upperLeftSource, Point upperLeftDestination, 大小 blockRegionSize) w Screenshoot_z_healem.Form1.backgroundWorker3_DoWork(Object sender, DoWorkEventArgs e)

我的 2 Backgroundworkers 代码看起来:

   private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            try
            {
                // Shot size = screen size
                Size shotSize = Screen.PrimaryScreen.WorkingArea.Size;

                // the upper left point in the screen to start shot
                // 0,0 to get the shot from upper left point
                int x = Convert.ToInt32(600);
                int y = Convert.ToInt32(600);
                Point upperScreenPoint = new Point(x, y);

                // the upper left point in the image to put the shot
                Point upperDestinationPoint = new Point(0, 0);

                int q = Convert.ToInt32(700) - x;
                int w = Convert.ToInt32(700) - y;
                // create image to get the shot in it
                Bitmap shot1 = new Bitmap(q, w, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                // new Graphics instance 
                Graphics graphics = Graphics.FromImage(shot1);

                // get the shot by Graphics class 
                graphics.CopyFromScreen(upperScreenPoint, upperDestinationPoint, shotSize);
                Bitmap b1 = shot;
                shot1.Dispose();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            };


        }
    }

    private void backgroundWorker4_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            try
            {
                // Shot size = screen size
                Size shotSize = Screen.PrimaryScreen.WorkingArea.Size;

                // the upper left point in the screen to start shot
                // 0,0 to get the shot from upper left point
                int x = Convert.ToInt32(600);
                int y = Convert.ToInt32(600);
                Point upperScreenPoint = new Point(x, y);

                // the upper left point in the image to put the shot
                Point upperDestinationPoint = new Point(0, 0);

                int q = Convert.ToInt32(700) - x;
                int w = Convert.ToInt32(700) - y;
                // create image to get the shot in it
                Bitmap shot2 = new Bitmap(q, w, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                // new Graphics instance 
                Graphics graphics = Graphics.FromImage(shot2);

                // get the shot by Graphics class 
                graphics.CopyFromScreen(upperScreenPoint, upperDestinationPoint, shotSize);
                Bitmap b2 = shot;
                shot2.Dispose();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            };

        }
    }

后台工作人员做同样的事情。获取屏幕区域的屏幕截图。

private void button3_Click(object sender, EventArgs e)
    {
        backgroundWorker3.RunWorkerAsync();
        backgroundWorker4.RunWorkerAsync();
    }

我应该用什么方法同时制作 2 个屏幕?也许我应该只启动 2 个程序并且不会出错?我知道我可以通过从主屏幕裁剪 1 个大图像来获得我想要的图像,但我需要这样做。

标签: c#multithreadingscreenshot

解决方案


推荐阅读