报错:对象当前正在其他地方使用
System.Drawing.Bitmap如果跨线程使用,或者同时被多方调用,就会报错对象当前正在其他地方使用
解决方案是新开线程就新建一个Bitmap副本,并且保证一个Bitmap对象同时只被一个地方使用
复现这个问题的例子如下:
string file="one image path";
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(file);
Task.Run(() =>
{
try
{
while (true)
{
for (int i = 0; i < bmp.Width; i++)
for (int j = 0; j < bmp.Height; j++)
{
var c1 = bmp.GetPixel(i, j);
}
Thread.Sleep(100);
}
}
catch (Exception ex)
{
}
});
Task.Run(() =>
{
try
{
while (true)
{
for (int i = 0; i < bmp.Width; i++)
for (int j = 0; j < bmp.Height; j++)
{
var c1 =