在WriteableBitmap的异步操作 [英] Asynchronous operations on WriteableBitmap
问题描述
我正在写在做位图上的集合,长时间操作WPF(C#)的应用程序。为了让我的应用程序响应,我决定用另一个线程来对位图执行的操作,并在主UI线程一个进度报告进度。我以为BackgroundWorker的会为我做什么,但看起来也不会那么容易。
I'm writing an application in WPF (C#) which does long operations on a collection of Bitmaps. To keep my application responsive, I decided to use another thread to perform the operations on bitmaps and report progress on a progressbar in main UI thread. I thought BackgroundWorker would do anything for me, but looks like it won't be that easy.
我有以下的code:
public class ImageProcessor
{
public Collection<WriteableBitmap> Pictures { get; private set; }
private BackgroundWorker _worker = new BackgroundWorker();
public ImageProcessor()
{
_worker.DoWork += DoWork;
}
public void DoLotsOfOperations()
{
_worker.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
// operations on Pictures collection
}
}
在运行时加载我使用一个标准的打开文件对话框到图片收集图像,然后我调用DoLotsOfOperations()方法。但只要我尝试访问任何一个位图的属性我得到InvalidOperationException异常:因为不同的线程拥有它调用线程不能访问对象
At runtime I load images using a standard open file dialog into the Pictures collection and then I invoke DoLotsOfOperations() method. But as soon as I try to access any of the properties of a single bitmap I get InvalidOperationException: "The calling thread cannot access the object because different thread owns it".
这是真正的obviosly - 我装的位图和填充集合中的UI线程,我尝试读取另一个线程收集要素。所以我尝试不同的方法:
It is obviosly true - I loaded bitmaps and populated the collection in the UI thread and I try to read collection elements in another thread. So i tried different approaches:
- 我通过整个集合作为的RunWorkerAsync方法的参数和从e.Argument的DoWork方法得到它回来,但后来当我试图读取一个位图的性能我还是得到了同样的异常。
- 我想同样的事情,这时候路过一个位图的BackgroundWorker的说法和我还是不能得到任何位图的性能,更别说位图的像素。
所以,我怎么能访问另一个线程(和pferably使用BackgroundWorker的$ P $)内的位图的数据?
So how can I access bitmap's data inside another thread (and preferably using BackgroundWorker)?
我不知道,也许我的整个做法是错误的。总的想法,我想实现的是:
I don't know, maybe my whole approach is wrong. The general idea I want to achieve is:
- 用户加载其位图,然后在窗口中显示出来。
- 用户点击一个按钮和位图长时间操作执行,但UI响应(允许用户例如取消调度研究)和进展报道陆侃栏上。
感谢您事先的任何帮助。
Thanks in advance for any help.
推荐答案
1)代理类(无螺纹限制)
1) Proxy class (without thread restriction)
public class WriteableBitmapProxy
{
public IntPtr BackBuffer { get; set; }
public int BackBufferStride { get; set; }
public int PixelHeight { get; set; }
public int PixelWidth { get; set; }
}
2)扩展方法(不安全)
2) Extension methods (unsafe)
public class RGBColor
{
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public uint Value
{
get
{
return (uint)(((uint)R << 16) + ((uint)G << 8) + (B) + ((uint)255 << 24));
}
}
}
public static RGBColor GetPixel(this WriteableBitmap bmp, uint x, uint y)
{
unsafe
{
if (y >= bmp.PixelHeight) return default(RGBColor);
if (x >= bmp.PixelWidth) return default(RGBColor);
// Get a pointer to the back buffer.
uint pBackBuffer = (uint)bmp.BackBuffer;
// Find the address of the pixel to draw.
pBackBuffer += y * (uint)bmp.BackBufferStride;
pBackBuffer += x * 4;
byte* pCol = (byte*)pBackBuffer;
return new RGBColor() { B = pCol[0], G = pCol[1], R = pCol[2] };
}
}
public static void SetPixel(this WriteableBitmapProxy bmp, uint x, uint y, RGBColor col)
{
SetPixel(bmp, x, y, col.Value);
}
public static void SetPixel(this WriteableBitmapProxy bmp, uint x, uint y, uint value)
{
unsafe
{
if (y >= bmp.PixelHeight) return;
if (x >= bmp.PixelWidth) return;
// Get a pointer to the back buffer.
uint pBackBuffer = (uint)bmp.BackBuffer;
// Find the address of the pixel to draw.
pBackBuffer += y * (uint)bmp.BackBufferStride;
pBackBuffer += x * 4;
// Assign the color data to the pixel.
*((uint*)pBackBuffer) = value;
}
}
3)过程火在不同的线程长时间运行的操作
3) Procedure to fire long running operation in different thread
var image = sender as Image;
var bitmap = image.Source as WriteableBitmap;
var prx = new WpfImage.MyToolkit.WriteableBitmapProxy()
{
BackBuffer = bitmap.BackBuffer,
BackBufferStride = bitmap.BackBufferStride,
PixelHeight = bitmap.PixelHeight,
PixelWidth = bitmap.PixelWidth
};
bitmap.Lock();
Thread loader = new Thread(new ThreadStart(() =>
{
Global_Histogramm(prx);
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { bitmap.AddDirtyRect(new Int32Rect(0, 0, prx.PixelWidth - 1, prx.PixelHeight - 1)); bitmap.Unlock(); }, null);
}
));
loader.Priority = ThreadPriority.Lowest;
loader.Start();
4)长时间运行操作执行
4) Long Running operation implementation
void Global_Histogramm(WpfImage.MyToolkit.WriteableBitmapProxy src)
{
int SrcX = src.PixelWidth;
int SrcY = src.PixelHeight;
double[] HR = new double[256];
double[] HG = new double[256];
double[] HB = new double[256];
double[] DR = new double[256];
double[] DG = new double[256];
double[] DB = new double[256];
uint i, x, y;
// wyzeruj tablice
for (i = 0; i < 256; i++) HB[i] = HG[i] = HR[i] = 0;
// wypelnij histogramy R G B
for (y = 0; y < SrcY; y++)
for (x = 0; x < SrcX; x++)
{
var color = src.GetPixel(x, y);
HB[color.B]++;
HG[color.G]++;
HR[color.R]++;
};
// oblicz histogramy znormalizowane i przygotuj dystrybuanty
int ilosc_punktow = SrcX * SrcY;
double sumaR = 0, sumaG = 0, sumaB = 0;
for (i = 0; i < 256; i++)
{
DB[i] = sumaB + HB[i] / ilosc_punktow;
DG[i] = sumaG + HG[i] / ilosc_punktow;
DR[i] = sumaR + HR[i] / ilosc_punktow;
sumaB = DB[i];
sumaG = DG[i];
sumaR = DR[i];
};
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { progressBar1.Maximum = SrcY - 1; }, null);
// aktualizuj bitmape
for (y = 0; y < SrcY; y++)
{
for (x = 0; x < SrcX; x++)
{
var stmp = src.GetPixel(x, y);
var val = new WpfImage.MyToolkit.RGBColor()
{
B = (byte)(DB[stmp.B] * 255),
G = (byte)(DG[stmp.G] * 255),
R = (byte)(DR[stmp.R] * 255)
};
src.SetPixel(x, y, val);
};
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { progressBar1.Value = y; }, null);
}
}
5)希望它证明的东西。
5) Hope it proves the thing.
这篇关于在WriteableBitmap的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!