步骤:

  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

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

  1. 分享一个安卓中异步获取网络图片并自适应大小的第三方程序(来自github)

    安卓中获取网络图片,生成缓存 用安卓手机,因为手机流量的限制,所以我们在做应用时,要尽量为用户考虑,尽量少耗点用户的流量,而在应用中网络图片的显示无疑是消耗流量最大的,所以我们可以采取压缩图片或者将图 ...

  2. node 创建静态web服务器(下)(处理异步获取数据的两种方式)

    接上一章. 上一章我们说创建的静态web服务器只能识别html,css,js文件,功能较为单一,且图片格式为text/html,这是不合理的. 本章,我们将解决该问题. 这里,我们先准备好一个json ...

  3. WPF 使用 SharpDx 异步渲染

    本文告诉大家如何通过 SharpDx 进行异步渲染,但是因为在 WPF 是需要使用 D3DImage 画出来,所以渲染只是画出图片,最后的显示还是需要 WPF 在他自己的主线程渲染 本文是一个系列,希 ...

  4. Curator 异步获取结果

    原声的ZooKeeper 的CRUD API有同步和异步之分,对于异步API,需要传递AsyncCallback回调.对于getData,getChildren,exists这三个API,还可以设置W ...

  5. iOS检测用户截屏并获取所截图片

    iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...

  6. js获取页面中图片的总数

    查看效果:http://keleyi.com/keleyi/phtml/image/9.htm 下面是完整代码: <html><body><div id="ke ...

  7. IOS: 使用imageIO获取和修改图片的exif信息

    使用imageIO获取和修改图片的exif信息 一幅图片除了包含我们能看见的像素信息,背后还包含了拍摄时间,光圈大小,曝光等信息.UIImage类将这些细节信息都隐藏了起来,只提供我们关心的图片尺寸, ...

  8. php获取网页中图片并保存到本地

    php获取网页中图片并保存到本地的代码,将网页中图片保存本地文件夹: save_img("http://www.jbxue.com" ?>

  9. jquery的异步获取返回值为中文时乱码解决方法

    用jqgrid异步获取列表值,遇到个问题,服务器端从数据库取到的数据没有出现中文乱码问题(日志打出来是没有乱码的),但是异步传到客户的时候却出现了乱码. 服务器端已经编码过了(UTF-8编码).开始一 ...

随机推荐

  1. iOS开发之复制字符串到剪贴板

    概述 一般有邀请复制链接需求功能,把字符串复制到系统剪贴板,供用户粘贴使用链接. 详细 代码下载:http://www.demodashi.com/demo/10714.html 一.主要思路 1.在 ...

  2. Spring MVC request flow

    1. When we enter a URL in the browser, the request comes to the dispatcher servlet.The dispatcher se ...

  3. 【LeetCode】114. Distinct Subsequences

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  4. SQL Server 2005/2008遍历所有表更新统计信息

    DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR 02   SELECT sst.name, 03          Schema_name(ss ...

  5. JVM性能监控

    有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 这些问题在日常开发中可能被很多人忽 ...

  6. PO_从PO追溯PR的方式和表(分析)

    2014-06-03 Created By BaoXinjian

  7. Okhttp常用方法示例

    这是我用到的一个util类 public class HttpBaseService { private OkHttpClient client = new OkHttpClient(); priva ...

  8. opencv源代码分析:icvGetTrainingDataCallback简单介绍

    /* *函数icvGetTrainingDataCallback介绍 *功能:对全部样本计算特征编号从first開始的num个特征,并保存到mat里. *输入: *CvMat* mat矩阵样本总数个行 ...

  9. Spring Cloud 概述

    1. Spring Cloud 引言 首先我们打开spring 的官网:https://spring.io/ 我们会看到这样一张图片 这个图片告诉我们,开发我们的应用程序就像盖楼一样, 首先我们需要搭 ...

  10. 使用VMware安装CentOS7步骤详情

    准备资料: CentOS-7-x86_64-Everything-1611 点击下载CentOS 对,资料就这些 第一步.  点击文件  再点击新建虚拟机 第二步 .点击完新建虚拟机之后会跳出一个窗口 ...