我想从网址下载GIF并保存到WP8的独立存储空间。我一直在尝试使用imagetools库解码GIF然后转换为jpeg,因为silverlight不支持GIF,我想稍后使用webbrowser控件显示保存的图像。我的代码:
ExtendedImage image = new ExtendedImage();
image.LoadingFailed += image_LoadingFailed;
image.LoadingCompleted += (sender, e) =>
{
var isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (filename.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
{
GifDecoder gifdecoder = new GifDecoder();
JpegEncoder jpegencoder = new JpegEncoder();
using (var stream = new IsolatedStorageFileStream("somefolder/" + filename, FileMode.Create, isoStore))
{
gifdecoder.Decode(image, stream);
BitmapImage bitmap = new BitmapImage();
using (MemoryStream stream2 = new MemoryStream())
{
jpegencoder.Encode(image, stream2);
bitmap.SetSource(stream2);
}
}
}
image.UriSource = new Uri(imageurl, UriKind.Absolute);
答案 0 :(得分:0)
如果将gif转换为jpeg,则会丢失动画。如果要使用Web浏览器控件显示图像,则根本不需要ImageTools。你可以做的是,从远程位置下载gif,将其保存到独立存储,然后通过将图像路径注入HTML,将其显示在Web浏览器控制器上。
所以,这是下载图像并将其保存到隔离存储的示例代码。
private string savedImagePath = string.Empty;
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.OpenReadCompleted += async (s, args) =>
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("savedGifName.gif",
FileMode.Create, storage))
{
await args.Result.CopyToAsync(fileStream);
savedImagePath = fileStream.Name;
}
}
};
client.OpenReadAsync(new Uri("http://www.example.com/someGIF.gif", UriKind.Absolute));
}
现在,您可以在savedImagePath中的隔离存储中找到映像的路径。要在Web浏览器控件上显示该图像,您可以执行以下操作:
private void ViewImageInWebBrowserControl(string ImageURL)
{
string backgroundColor = "<body bgcolor=\"#000000\">"; // enter different hex value for different background color
string imageHTML = "<html><head><meta name=\"viewport\" " +
"content=\"width=440\" id=\"viewport\" />" +
"</head>" + backgroundColor + "<IMG SRC=\"" +
ImageURL + "\"height=\"300\" width=\"300\"></body></html>";
browserControl.NavigateToString(imageHTML); // browserControl is created in XAML which is not shown here
}
然后调用ViewImageInWebBrowserControl(savedImagePath);构建html并在Web浏览器控件中显示gif(现在位于isolted存储中)。
希望这会有所帮助。