我知道如何使用以下方法在隔离存储中保存图像:
private void addButton_Click(object sender, RoutedEventArgs e)
{
MemoryStream stream = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(myImage, null);
BitmapImage bi = new BitmapImage();
wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
string data = Convert.ToBase64String(stream.GetBuffer());
appSettings.Add("image", data);
}
我知道如何使用以下方法加载它:
private void loadImage_Click(object sender, RoutedEventArgs e)
{
byte[] imageBytes = Convert.FromBase64String(appSettings["image"].ToString());
MemoryStream ms = new MemoryStream(imageBytes);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
myImage.Source = bitmapImage;
}
但我不知道如何从URL加载和读取它,如何实现呢?
感谢您的帮助。
答案 0 :(得分:1)
WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) =>
{
byte[] imageBytes = new byte[e.Result.Length];
e.Result.Read(imageBytes, 0, imageBytes.Length);
// Now you can use the returned stream to set the image source too
var image = new BitmapImage();
image.SetSource(e.Result);
NLBI.Thumbnail.Source = image;
};
client.OpenReadAsync(new Uri(article.ImageURL));
编辑:这里有关于OpenReadComplete(MSDN)以及如何使用它的更多信息