【WPF】【C#】联网异步获取二进制文件(如图片)的流程

来源:互联网 发布:余弦相似度算法 java 编辑:程序博客网 时间:2024/05/16 17:00

步骤:

  1. 联网异步获取Json数据。
  2. 使用Json.NET工具,反序列化Json为对应的实体类,获得该实体类的对象。
  3. 从对象身上获取图片路径(实体类中定义了头像图片是string类型的文件路径)。
  4. 根据图片路径,再次联网异步获取图片。
  5. 将二进制资源转换为合适的类型(可能需要多步转换)。
  6. 给控件使用。
public partial class MyWindow : Window{    public MyWindow()    {        InitializeComponent();        // 初始化设计师的个人信息        InitDesignerInfo();    }    private async void InitDesignerInfo()    {        // 1、联网异步获取Json数据        string uri = "资源在服务器的路径";        string json = await SystemUtils.GetJsonDataFromWebServerAsync(uri);   // 使用Json.NET三方库        // 2、反序列化Json为对应的实体类        DesignerInfo info = JsonConvert.DeserializeObject<DesignerInfo>(json); // DesignerInfo实体类        // 3、从对象身上获取图片路径        string avatarPath = info.Avatar;        // 4、再联网异步获取头像图片        Task<System.Drawing.Image> result = SystemUtils.GetBitmapFromWebServerAsync(avatarPath);        System.Drawing.Image image = await result;        // 5、转型:Image --> Bitmap --> BitmapImage        Bitmap bitmap = new Bitmap(image);        BitmapImage bitmapImage = SystemUtils.BitmapToBitmapImage(bitmap);        // 给控件使用资源        avatar.Source = bitmapImage;    }}

把一些通用的操作单独抽取作为一个工具类

public class SystemUtils{    /// <summary>    /// 异步方法:联网从服务端获取Json数据    /// </summary>    /// <param name="uri">资源的绝对路径(服务器IP + 资源的相对路径)</param>    /// <returns>资源的内容</returns>    public static async Task<string> GetJsonDataFromWebServerAsync(string uri)    {        // 参考:https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx        WebClient client = new WebClient();        string result = await client.DownloadStringTaskAsync(new Uri(uri));        return result;    }    /// <summary>    /// 异步方法:联网获取Bitmap二进制数据    /// </summary>    /// <param name="uri">资源的绝对路径(服务器IP + 资源的相对路径)</param>    /// <returns>Bitmap图片</returns>    public static async Task<Image> GetBitmapFromWebServerAsync(string uri)    {        // 参考:https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx        WebClient client = new WebClient();        byte[] result = await client.DownloadDataTaskAsync(new Uri(uri));        Image image = SystemUtils.ByteArrayToImage(result);        return image;    }    /// <summary>      /// byte[] --> Image      /// </summary>      /// <param name="byteArrayIn">二进制图片流</param>      /// <returns>System.Drawing.Image</returns>      public static System.Drawing.Image ByteArrayToImage(byte[] byteArrayIn)    {        if (byteArrayIn == null)            return null;        using (MemoryStream ms = new MemoryStream(byteArrayIn))        {            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);            ms.Flush();            return image;        }    }    /// <summary>    /// Bitmap --> BitmapImage    /// </summary>    /// <param name="bitmap"></param>    /// <returns>BitmapImage</returns>    public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)    {        using (MemoryStream stream = new MemoryStream())        {            bitmap.Save(stream, ImageFormat.Bmp);            stream.Position = 0;            BitmapImage result = new BitmapImage();            result.BeginInit();            // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."            // Force the bitmap to load right now so we can dispose the stream.            result.CacheOption = BitmapCacheOption.OnLoad;            result.StreamSource = stream;            result.EndInit();            result.Freeze();            return result;        }    }}

关于Bitmap和BitmapImage等图片相关类的转换方法:

http://blog.csdn.net/wangshubo1989/article/details/47296339
http://blog.csdn.net/qq_18995513/article/details/53693554

更多相关内容:
谷歌搜关键字 C# WPF Convert Bitmap BitmapImage

0 0