想法是处理加载的图像。我首先想要以这种方式处理它,在图像中有一条白/黑线。 实际问题是,新的Thread(用于Thread.Sleep)在处理时冻结整个UI。当我尝试Dispatcher时,图像不会更新。
所以我得到了ProcessingClass:
public WriteableBitmap GetProcessedPicture(int i)
{
WriteableBitmap wb = image.image;
Process(ref wb, i);
return wb;
}
private void Process(ref WriteableBitmap wb, int j)
{
int stride = wb.PixelWidth * (wb.Format.BitsPerPixel + 7) / 8;
byte[] data = new byte[stride * wb.PixelHeight];
for (int i = 0; i < data.Length; i++) data[i] = 255;
//just to see some changes
wb.WritePixels(new Int32Rect(j, 0, 100, 100), data, stride, 0);
}
MainViewModel类(我使用MVVM灯)和:
public void Start_Click()
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
for (int i = 0; i < 100; i++)
{
ImageSource = processingClass.GetProcessedPicture(i);
Thread.Sleep(100);
}
}));
};
感谢您的帮助。
答案 0 :(得分:0)
我认为ImageSource
(假设它绑定到Image.Source)不接受WriteableBitmap
。您必须先将其转换为BitmapImage
!
调整你的代码我会做类似的事情:
public void Start_Click()
{
var t = new Task(() =>
{
for (var i = 0; i < 100; i++)
{
var writeablebitmap = processingClass.GetProcessedPicture(i);
var bitmapImage = processingClass.ConvertToBitmapImage(writeablebitmap);
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
ImageSource = bitmapImage;
}));
Thread.Sleep(100);
}
});
t.Start();
}
这样您就不会停止使用图像处理的UI线程,但仍然会在UI线程上更新图像。
至于将WriteableBitmap
转换为BitmapImage
,互联网上有大量资源,例如this one