c# - 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 个大图像来获得我想要的图像,但我需要这样做。
解决方案
推荐阅读
- sockets - SocketIO 没有监听“on”函数
- php - 如何在新的分页符中重复表头
- reactjs - 在深层子级或配置文件中访问顶级道具的最高效方法是什么?
- reactjs - setInterval 中的 setState 未按预期运行
- javascript - 尝试将数组的元素添加在一起
- c - 为什么竞争条件发生在循环中?
- spring-boot - 如何在 Eclipse 中从我的应用程序中打开源代码文件?
- javascript - 在 JS 中迭代 JSON 结构
- javascript - 事件处理函数中的变量不是动态解析的。为什么?
- python - 如何在 crontab 中配置项目和 virtualenv 路径以在 Django 中安排作业?