c#在2个线程中同时使用graphics.CopyFromScreen

时间:2018-09-14 07:11:23

标签: c# multithreading screenshot

我想同时制作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,Size blockRegionSize)w   Screenshoot_z_healem.Form1.backgroundWorker3_DoWork(对象发送方,   DoWorkEventArgs e)

我为2个背景工作人员编写的代码如下:

   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个程序,并且不会出错? 我知道我可以从主屏幕上通过裁切1大图像获得想要的图像,但是我需要那样做。

0 个答案:

没有答案