WritableBitmap改变后界面中的图片没有更新 [英] Image in the interface is not updated after WritableBitmap has changed

查看:8
本文介绍了WritableBitmap改变后界面中的图片没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次移动鼠标指针时,我都试图通过绘制圆圈来更新图像.pixelBuffer 存储在 map 对象中,并使用 CircleFilledWithGradientMethod() 更新.我将像素缓冲区加载到 WriteableBitmap Bitmap,然后尝试通过将 XAML UI 中的图像元素的图像源设置为 Bitmap 来显示它.发生的情况是,当我第一次移动鼠标时,我看到绘制的前两个圆圈,但是当我移动鼠标时,图像没有更新.为什么会这样?有没有我应该调用 Image 的方法来强制它重绘?我使用 stream.WriteAsync 两次的原因,当我可以只使用一次时,只是为了表明它工作正常.

 private async void TextArea_PointerMoved(object sender, PointerRoutedEventArgs e){像素 pixel1 = 新像素 (255, 0, 0);像素 pixel2 = 新像素 (255, 255, 255);map.CircleFilledWithGradient((int) pointer.Position.X, (int)pointer.Position.Y, 100, pixel1, pixel2);使用 (流流 = Bitmap.PixelBuffer.AsStream()){stream.WriteAsync(map.pixelData, 0, map.pixelData.Length);}map.CircleFilledWithGradient((int)pointer.Position.X+100, (int)pointer.Position.Y, 100, pixel1, pixel2);使用 (流流 = Bitmap.PixelBuffer.AsStream()){stream.WriteAsync(map.pixelData, 0, map.pixelData.Length);}this.Image.Source = 位图;}

我认为正在发生的是,一旦我在屏幕上绘制图像,它就会缓存它,然后继续使用旧图像.还是标准的Image元素不支持重绘?

更新:我的 Bitmap 是一个私有类变量.如果我这样做

WriteableBitmap Bitmap = new WriteableBitmap((int)this.ActualWidth, (int)this.Grid.RowDefinitions[1].ActualHeight);

一切都开始工作了,但那是不是内存效率低下?每次移动鼠标时,我不是为每个新的 WriteableBitmap 分配内存吗?正如我发现的那样,问题肯定与 Image 组件有关.为什么当我只是更改它的源时它不会更新,但当我将其源更改为不同的对象时会更新.`

解决方案

有没有我应该调用 Image 来强制它重绘的方法?

关闭.WriteableBitmap 上有一个方法.

调用 WriteableBitmap.Invalidate 在您更新它以请求刷新后.

假设已经将 Bitmap 设置为 Image.Source,替换该行:

 this.Image.Source = Bitmap;

与:

 Bitmap.Invalidate();

<块引用>

为什么当我只是更改其源时它不会更新,但当我将其源更改为不同的对象时会更新.

将 Image.Source 设置为它已经设置的相同内容是一个 noop.大多数复杂的属性会忽略不会更改现有值的更改".每次创建一个新的 WriteableBitmap 都有效(效率很低),因为它是不同的.

I am trying to update the image by drawing circles every time mouse pointer is moved. The pixelBuffer is stored in map object and updated with CircleFilledWithGradientMethod(). I load the pixel buffer in to WriteableBitmap Bitmap and then try to display it by setting image source of an image element in my XAML UI to Bitmap. What happens, is that when I first move my mouse, I see the very first 2 circles drawn but then as I move the mouse, the image does not get updated. Why could that be? Is there a method I should call on Image to force it to redraw? The reason I use stream.WriteAsync two times, when I could have just used it one, is just to show that it is working properly.

  private async void TextArea_PointerMoved(object sender, PointerRoutedEventArgs e)
            {
                Pixel pixel1 = new Pixel(255, 0, 0);
                Pixel pixel2 = new Pixel(255, 255, 255);

                map.CircleFilledWithGradient((int) pointer.Position.X, (int)pointer.Position.Y, 100, pixel1, pixel2);

                using (Stream stream = Bitmap.PixelBuffer.AsStream())
                {
                    stream.WriteAsync(map.pixelData, 0, map.pixelData.Length);
                }
                map.CircleFilledWithGradient((int)pointer.Position.X+100, (int)pointer.Position.Y, 100, pixel1, pixel2);

                using (Stream stream = Bitmap.PixelBuffer.AsStream())
                {
                    stream.WriteAsync(map.pixelData, 0, map.pixelData.Length);
                }
                this.Image.Source = Bitmap;
            }

What I think is happening, is that as soon as I draw the image on the screen, it caches it, and then keeps using the old image. Or is it that the standard Image element does not support redrawing?

Update: My Bitmap was a private class variable. If I do

WriteableBitmap Bitmap = new WriteableBitmap((int)this.ActualWidth, (int)this.Grid.RowDefinitions[1].ActualHeight);

It all starts to work, but isn't that memory inefficient? Aren't I allocating memory for each new WriteableBitmap each time I move my mouse. And as I have found out, the issue is definitely with with Image component. Why wouldn't it update when I just make changes to it's source, but updates when I change its source to a different object. `

解决方案

Is there a method I should call on Image to force it to redraw?

Close. There's a method on WriteableBitmap.

Call WriteableBitmap.Invalidate after you update it to request the refresh.

Assuming the Bitmap was already set as Image.Source, replace the line:

 this.Image.Source = Bitmap;

With:

 Bitmap.Invalidate();

Why wouldn't it update when I just make changes to its source, but updates when I change its source to a different object.

Setting the Image.Source to the same thing it's already set to is a noop. Most complex Properties ignore "changes" which don't change the existing value. Creating a new WriteableBitmap each time works (very inefficiently) because it's different.

这篇关于WritableBitmap改变后界面中的图片没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆