我知道位图不是线程安全的,因此使用此解决方法
Bitmap[] blobCopies = MakeBlobCopies(bmpBlob, 2);
BackgroundWorker bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerAsync(blobCopies[0]);
BackgroundWorker bw2 = new BackgroundWorker();
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork);
bw2.RunWorkerAsync(blobCopies[1]);
DisposeBlobCopies(blobCopies);
在 Backgroundwork 事件句柄中,我对位图应用了一些过滤器,但我得到了访问冲突异常
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Drawing
StackTrace:
at System.Drawing.SafeNativeMethods.Gdip.GdipBitmapLockBits(HandleRef bitmap, GPRECT& rect, ImageLockMode flags, PixelFormat format, BitmapData lockedBitmapData)
at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format)
我什至没有处理位图(我需要避免阻塞内存)。但是仍然得到这个例外。我不需要从工作线程返回任何东西,这是否可以立即处理?有任何想法吗?
private Bitmap[] MakeBlobCopies(Bitmap originalBitmap, int n)
{
Bitmap[] blobCopies = new Bitmap[n];
for (int i = 0; i < n; i++)
{
blobCopies[i] = (Bitmap)originalBitmap.Clone();
}
return blobCopies;
}