在图像控件中显示位图值

4

我有一张位图,我想在图像控件中显示它,而不保存它,我该怎么做?

3个回答

2

将位图转换为 BitmapImage,并将其分配给属性(例如在示例中为 CurrentImage),该图像控件的 Source 属性绑定到:

MemoryStream ms = new MemoryStream();
_bitmap.Save(ms, ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
CurrentImage = bi;

1
你有位图的方式是什么?如果你有一个BitmapImage,只需设置Image.Source依赖属性即可。
BitmapImage bitmap = ... // whatever
Image image = new Image();
image.Source = bitmap;

在XAML中,您可以将其设置为磁盘上的某个内容,或者绑定它。
<Image Source="myimage.bmp"/>
// Or ...
<Image Source="{Binding Path=PropertyInMyViewModel}"/>

1

BitmapImage 可以将 uri 作为参数:

BitmapImage MyImage = new BitmapImage(someuri);

然后您可以将图像控件绑定到它上面

<Image Source={Binding MyImage}/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接