display bitmap value in image control
I have a bitmap and I want to display it in image control without saving it, 开发者_JAVA百科how can I do this?
Convert the bitmap to a BitmapImage
and assign it to the property (i.e. in the example CurrentImage
) the image control's Source
property is bound to:
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;
In what way do you have the bitmap? If you a BitmapImage
, just set the Image.Source dependency property.
BitmapImage bitmap = ... // whatever
Image image = new Image();
image.Source = bitmap;
In XAML you can set it to something on disk also, or bind it.
<Image Source="myimage.bmp"/>
// Or ...
<Image Source="{Binding Path=PropertyInMyViewModel}"/>
A BitmapImage
can take an uri as parameter:
BitmapImage MyImage = new BitmapImage(someuri);
and then you can bind your image control to it
<Image Source={Binding MyImage}/>
精彩评论