Silverlight: Setting an Image source to MemoryStream
My application receives a jpe开发者_运维问答g file as a MemoryStream
from a webservice. I need assign the image to image.Source. How can this be done?
In here it is described:
void proxy_GetImageCompleted(object sender, GetImageCompletedEventArgs e)
{
MemoryStream stream = new MemoryStream(e.Result);
BitmapImage b = new BitmapImage();
b.SetSource(stream);
imgImage.Source = b;
}
The System.Windows.Controls.Image.Source property is of type System.Windows.Media.ImageSource A System.Windows.Media.Imaging.BitmapImage is derived from ImageSource.
And BitmapImage has a SetSource(Stream streamsource) method.
So with the following code you can add an image from a stream:
Stream inStream = [your MemoryStream];
BitmapImage tempImage = new BitmapImage();
tempImage.SetSource(inStream);
YourControlsImage.Source = tempImage;
MSDN links:
Image.Source Property
BitmapSource.SetSource Method
BitmapImage Class
精彩评论