WP8:下载并保存 GIF 到独立存储 [英] WP8: Download and save GIF to isolated storage

查看:29
本文介绍了WP8:下载并保存 GIF 到独立存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 URL 下载 GIF 并保存到 WP8 的独立存储.我一直在尝试使用 imagetools 库来解码 GIF,然后转换为 jpeg,因为 Silverlight 不支持 gif,我想稍后使用 webbrowser 控件显示保存的图像.我的代码:

I want to download a GIF from a URL and save to isolated storage for WP8. I have been trying to use the imagetools library to decode the GIF then convert into a jpeg as silverlight does not support gifs and i want to display the saved image later using the webbrowser control. My code:

        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);

推荐答案

如果将 gif 转换为 jpeg,您将丢失动画.如果您打算使用 Web 浏览器控件来显示图像,则根本不需要 ImageTools.您可以做的是,从远程位置下载 gif,将其保存到独立存储,然后通过将图像路径注入 HTML 将其显示在 Web 浏览器控制器上.

If you convert a gif to jpeg, you will lose the animation. If you are going to use a web browser control to display the image, you do not need ImageTools at all. What you can do is, download the gif from the remote location, save it to isolated storage and then display it on a web browser controller by injecting the image path into HTML.

因此,这里是下载图像并将其保存到独立存储的示例代码.

So, here's sample code to download the image and save it to isolated storage.

    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 浏览器控件上显示该图像,您可以执行以下操作:

Now, you have the path to the image in isolated storage in savedImagePath. In order to display that image on a web browser control, you can do the following:

    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(现在位于隔离存储中).

Then call ViewImageInWebBrowserControl(savedImagePath); to build the html and display the gif (now located in isolted storage) in a web browser control.

希望这会有所帮助.

这篇关于WP8:下载并保存 GIF 到独立存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆