I try 1000 times, to convert a simple stream (http webresponse) to bitmapimage, but no one tutorial is working in c# windows 8.
Example:
BitmapImage image = new BitmapImage();
image.SetSource(stream);
image1.Source = image;
Thank's for all reply.
Solution
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes((byte[])command);
await writer.StoreAsync();
BitmapImage image = new BitmapImage();
image.SetSource(randomAccessStream);
Have you tried this?
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes(response.Content.ReadAsByteArray());
BitmapImage image = new BitmapImage();
image.SetSource(randomAccessStream);
Try this code:
private async Task GetLocalImageAsync(string internetUri, string folderName,
string uniqueName)
{
using (var response = await HttpWebRequest.CreateHttp(internetUri)
.GetResponseAsync())
{
using (var stream = response.GetResponseStream())
{
var folder = await ApplicationData.Current.LocalFolder
.CreateFolderAsync(folderName,
CreationCollisionOption.OpenIfExists);
var file = await folder.CreateFileAsync(
string.Format("{0}", uniqueName),
CreationCollisionOption.ReplaceExisting);
using (var filestream = await file.OpenStreamForWriteAsync())
{
await stream.CopyToAsync(filestream);
await filestream.FlushAsync();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With