CompoentOne WinForms C1FlexGrid 延迟加载网络图片

日常使用C1FlexGrid的场景非常多,其中有这样一个场景:"c1flexgrid显示网络图片问题--cell显示网络图片显得太卡  主要是把网络图片下载到本地需要时间,每行显示一张图,行数多了,速度慢的更不行 "针对这个场景,有如下3个解决方案。

发布于 2013/10/26 00:00

ComponentOne Enterprise

 

日常使用C1FlexGrid的场景非常多,其中有这样一个场景:

"c1flexgrid显示网络图片问题--cell显示网络图片显得太卡

主要是把网络图片下载到本地需要时间,每行显示一张图,行数多了,速度慢的更不行 "

针对这个场景,有如下3个解决方案:

方案一: 启动多线程下载图片,每个图片下载完成后,再逐个更新cell

方案二: 默认显示一个本地图片,当用户点击cell再去下载图片。 (推荐的方案)

方案三: 如果知道是那些图片,可以预先缓存下来图片。

 

编程实现思路如下:

首先:c1flexgrid支持显示图片的,不需要单独添加C1Picturebox控件

其次:采用点击事件方式,延迟实现加载网络图片。 为了提升用户体验,可以显示一个预览图占位。

最后:添加一个缓存,判断哪些row、col已经加载过picture了。

 

代码1---初始化FlexGrid:

  1: private void Form1_Load(object sender, EventArgs e)

  2:         {

  3:             c1FlexGrid1.Clear();

  4:             if (c1FlexGrid1.Rows.Count > 0)

  5:             {

  6:                 c1FlexGrid1.Rows.RemoveRange(0, c1FlexGrid1.Rows.Count);

  7:             }

  8: 

  9:             if (c1FlexGrid1.Cols.Count > 0)

 10:             {

 11:                 c1FlexGrid1.Cols.RemoveRange(0, c1FlexGrid1.Cols.Count);

 12:             }

 13: 

 14:             //构建Grid

 15:             for (int i = 0; i < 5; i++)

 16:             {

 17:                 Column imageCol = c1FlexGrid1.Cols.Add();

 18:                 imageCol.Caption = "Images" + i;

 19:                 imageCol.DataType = typeof(Image);

 20:                 imageCol.ImageAlign = ImageAlignEnum.CenterCenter;

 21:                 imageCol.Width = 200;

 22:             }

 23: 

 24:             string path = AppDomain.CurrentDomain.BaseDirectory + @"..\..\newlogo.png";

 25:             for (int i = 0; i < 10; i++)

 26:             {

 27:                 Row row = c1FlexGrid1.Rows.Add();

 28:                 row.Height = 200;

 29: 

 30:                 for (int j = 0; j < 5; j++)

 31:                 {

 32:                     row[j] = Image.FromFile(path);

 33:                 }

 34:             }

 35:         }

 

代码2--点击加载图片:

  1:  private string grapeURL = "http://www.grapecity.com.cn/company/img/xiangrapecity.jpg";

  2:         private void c1FlexGrid1_Click(object sender, EventArgs e)

  3:         {

  4:             int row = c1FlexGrid1.HitTest().Row;

  5:             int col = c1FlexGrid1.HitTest().Column;

  6: 

  7:             string key = string.Format("{0}_{1}", row, col);

  8:             if (haveLoadPicture.ContainsKey(key))

  9:             {

 10:                 return;

 11:             }

 12: 

 13:             //加载网络图片

 14:             c1FlexGrid1[row, col] = LoadPicture(row, col);

 15: 

 16:             haveLoadPicture.Add(key, true);

 17:         }

 18: 

 19:         private Image LoadPicture(int row, int col)

 20:         {

 21:             WebClient wc = new WebClient();

 22: 

 23:             //使用网络代理上网---取决你的公司是否能直接上网

 24:             if (checkBox1.Checked)

 25:             {

 26:                 wc.Proxy = new WebProxy("xaproxy", 888)

 27:                 {

 28:                     Credentials = new NetworkCredential("username", "password")

 29:                 };

 30:                 

 31:             }

 32: 

 33:             string tempFile = string.Format("{0}{1}.jpg", AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.Ticks);

 34:             wc.DownloadFile(grapeURL, tempFile);

 35: 

 36:             return Image.FromFile(tempFile);

 37:         }

 38: 

 39:         private Dictionary<string, bool> haveLoadPicture = new Dictionary<string, bool>();

 

截图如下--点击图片即可呈现网络图片:

Demo3

-------------------------------------------------------------------------------------------------------------------------

C#demo源码如下:

Winform_C1FlexGrid_DelayLoadNetworkPicture.rar

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态