DataGridView控件利用WebClient异步加载并显示网络上的图片

来源:互联网 发布:windows 画图工具 编辑:程序博客网 时间:2024/05/13 20:50

  这两天就这个问题,把我给弄得个纠结啊!最后跟大家分享一下!当然会有BUG,请海涵我这个菜鸟,并给予指示!

  最开始的时候,绑定数据加载图片,添加一个图片列,通过url地址下载显示出来,正确!如下图:

写的代码如下:

webservice的异步操作:

public int image_i = 0;      //结果代表DataTable的行数

void service_GetListCompleted(object sender, GetListCompletedEventArgs e)
        {
            if (!e.Result)
            {
                MessageBox.Show(e.msg,  "错误信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            dtgvw_RecipesList.DataSource = e.dt;
            if (!DataGridView1.Columns.Contains("图片"))
            {
                DataGridViewColumn column = new DataGridViewImageColumn();
                column.Name = "图片";
                DataGridView1.Columns.Insert(1, column);       //添加新DataGridView列
                DataGridView1.Columns["图片"].Width = 100;
            }
            if (e.dt.Rows.Count <= 0)
                return;
            image_i = 0;     //此处一定要归零,否则,在添加编辑之后重新加载的时候就会从最后一行+1开始,直接就报错over了
            dt = e.dt;
            WebClient wc = new WebClient();
            if (e.Error == null && !e.Cancelled)
            {
                wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
                wc.DownloadDataAsync(new Uri(dt.Rows[image_i]["R_Image"].ToString()));
            }
        }

void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                byte[] bytes = e.Result;
                MemoryStream ms = new MemoryStream(bytes);
                Image image = Image.FromStream(ms);

                DataGridView1.Rows[image_i].Height = 100;
                DataGridView1.Rows[image_i].Cells["图片"].Value = image;
                image_i++;
                if (image_i >= dtgvw_RecipesList.Rows.Count)
                    return;
                WebClient wc = new WebClient();
                if (e.Error == null && !e.Cancelled)
                {
                    wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
                    wc.DownloadDataAsync(new Uri(dt.Rows[image_i]["Image"].ToString()));
                }
            }
        }

这样写出来,就是我上面图片展示的结果,但是,假如其中有一个图片的URL地址错误不能加载出来,那么它之后的图片都不会再加载了,如果不写 if (e.Error == null && !e.Cancelled)这段判断一下,在byte[] bytes = e.Result;这行就会报错,所以,菜鸟我想出来的办法就是:

  if (e.Error == null && !e.Cancelled)
            {
                wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
                wc.DownloadDataAsync(new Uri(dt.Rows[image_i]["Image"].ToString()));
            }

改成

try

            {

                wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
                wc.DownloadDataAsync(new Uri(dt.Rows[image_i]["Image"].ToString()));

            }

catch

           {

               mage_i++;
                WebClient wc = new WebClient();      //如果之前定义过久无需再次定义了,此处再次说一个编程中遇到的问题(如果把WebClient 设置成全局变量,在异步时会报一个WebClient 不支持并发I/O操作,所以我就多次定义了)
                wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);
                wc.DownloadDataAsync(new Uri(dt.Rows[image_i]["Image"].ToString()));

           }

无论在加载当前那行的图片是url地址错误还是其他任何错误都执行下一行,可以解决掉很多附带的其他问题!



------------------------------------------------------------------------一个菜鸟的工作总结

  




原创粉丝点击