简体   繁体   English

WPF image.source 在运行时更改,来自 byte[]。 但图像是空白的(白色)

[英]WPF image.source change during runtime, from byte[]. But image is blank(white)

I am trying to change an image for image1 during runtime.我正在尝试在运行时更改image1的图像。 However the image turns blank(blank), what am I doing wrong?但是图像变成空白(空白),我做错了什么?

ImageAsBytes is a Byte[] containing an image. ImageAsBytes是一个包含图像的Byte[] ScrollViewer1 is the where image1 is located. ScrollViewer1image1所在的位置。

using (MemoryStream ms = new MemoryStream(ImagesAsBytes, 0, ImagesAsBytes.Length))
            {
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = ms;
                image.EndInit();

            }
image1.Source = image; 
scrollViewer1.UpdateLayout();

I think your Image can't be displayed because the MemoryStream you are using is being disposed.我认为您的图像无法显示,因为您正在使用的 MemoryStream 正在被处理。 Remove the surrounding using block and see if that helps. using块移除周围的环境,看看是否有帮助。 (you would need to dispose of the stream manually if you don't need it anymore then) (如果您不再需要 stream,则需要手动处理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM