C# 对可写位图像素的更改不';t更新屏幕
我在连接到图像控件的Windows Phone 8应用程序中有一个C# 对可写位图像素的更改不';t更新屏幕,c#,windows-phone-8,writeablebitmap,C#,Windows Phone 8,Writeablebitmap,我在连接到图像控件的Windows Phone 8应用程序中有一个WriteableBitmap。我在图像的每一行中循环,一次异步绘制一行像素,然后安排下一行进行绘制。但是,更改基础像素数据似乎不会触发已更改的属性,因此控件不会被更新。如果我将图像源设置为从相同像素创建的新WriteableBitmap,图像会很好地更新,但我做了大量的数组复制 void PaintImage(object state) { // get my height, width, row, etc. from
WriteableBitmap
。我在图像的每一行中循环,一次异步绘制一行像素,然后安排下一行进行绘制。但是,更改基础像素数据似乎不会触发已更改的属性,因此控件不会被更新。如果我将图像源设置为从相同像素创建的新WriteableBitmap,图像会很好地更新,但我做了大量的数组复制
void PaintImage(object state)
{
// get my height, width, row, etc. from the state
int[] bitmapData = new int[width];
// load the data for the row into the bitmap
Dispatcher.BeginInvoke(() =>
{
var bitmap = ImagePanel.Source as WriteableBitmap;
Array.Copy(bitmapData, 0, bitmap.Pixels, row * width, bitmapData.Length);
if (row < height - 1)
{
var newState = ... // create new state
ThreadPool.QueueUserWorkItem(PaintImage, newState);
}
});
}
似乎我需要手动让WriteableBitmap触发一些属性更改通知,以便包含它的图像。我猜如果我将图像绑定到ViewModel中的可写位图上,这个问题就会消失。我想你应该调用Invalidate()请求重画。Ref:只需添加一个脏矩形
_myBitmap.Lock();
_myBitmap.AddDirtyRect(new Int32Rect(0, 0, _myBitmap.PixelWidth, _myBitmap.PixelHeight));
_myBitmap.Unlock();
或者如果你在后台线程上
Application.Current.Dispatcher.InvokeAsync(() =>
{
_myBitmap.Lock();
_myBitmap.AddDirtyRect(new Int32Rect(0, 0, _myBitmap.PixelWidth, _myBitmap.PixelHeight));
_myBitmap.Unlock();
});
难道你不能等到整个图片完成绘制,然后用新的更新图片替换整个imageSource吗?不,这不是我希望它发挥作用的方式。然后最简单的方法可能是将它绑定到VM,并在每次刷新图片时执行RaisePropertyChanged()。是的,我在最初的问题中提到了这一点,事实上,它与虚拟机一起工作。我很好奇是否可以手动刷新控件并根据新数据重新绘制。
Application.Current.Dispatcher.InvokeAsync(() =>
{
_myBitmap.Lock();
_myBitmap.AddDirtyRect(new Int32Rect(0, 0, _myBitmap.PixelWidth, _myBitmap.PixelHeight));
_myBitmap.Unlock();
});