DevExpress GridView表格CustomUnboundColumnData事件异步加载图片
DevExpress GridView表格CustomUnboundColumnData事件异步加载图片
扫一扫加微信
图片列设置
1. 图片列的ColumnEdit属性设置为RepositoryItemPictureEdit组件。
repImage.Name = "repImage";
repImage.NullText = " ";
2. 图片列的UnboundType属性设置为Object
窗体定义一个变量images
C# Code:
Dictionary<string, AsynDownImage> images = new Dictionary<string, AsynDownImage>();
//来源:C/S框架网 | www.csframework.com | QQ:23404761
//来源:C/S框架网 | www.csframework.com | QQ:23404761
CustomUnboundColumnData事件:
C# Code:
private void gvSummary_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if ((e.Column.Name == "col_Last1_mainImageUrl" || e.Column.Name == "col_Last2_mainImageUrl") && e.IsGetData)
{
string imgurl = "";
switch (e.Column.Name)
{
case "col_Last1_mainImageUrl":
imgurl = (e.Row as DataRowView).Row["Last1_mainImageUrl"].ToStringEx();
break;
case "col_Last2_mainImageUrl":
imgurl = (e.Row as DataRowView).Row["Last2_mainImageUrl"].ToStringEx();
break;
}
if (String.IsNullOrEmpty(imgurl))
{
e.Value = null;
return;
}
if (imgurl.Substring(0, 2) == "//") imgurl = "https:" + imgurl;
if (images.ContainsKey(imgurl))
{
images[imgurl].AddListener(e);
}
else
{
AsynDownImage down = new AsynDownImage(imgurl, this);
down.AddListener(e);
images[imgurl] = down;
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
if ((e.Column.Name == "col_Last1_mainImageUrl" || e.Column.Name == "col_Last2_mainImageUrl") && e.IsGetData)
{
string imgurl = "";
switch (e.Column.Name)
{
case "col_Last1_mainImageUrl":
imgurl = (e.Row as DataRowView).Row["Last1_mainImageUrl"].ToStringEx();
break;
case "col_Last2_mainImageUrl":
imgurl = (e.Row as DataRowView).Row["Last2_mainImageUrl"].ToStringEx();
break;
}
if (String.IsNullOrEmpty(imgurl))
{
e.Value = null;
return;
}
if (imgurl.Substring(0, 2) == "//") imgurl = "https:" + imgurl;
if (images.ContainsKey(imgurl))
{
images[imgurl].AddListener(e);
}
else
{
AsynDownImage down = new AsynDownImage(imgurl, this);
down.AddListener(e);
images[imgurl] = down;
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
GridView异步加载图片,在UnboundColumn列显示图片
C# Code:
/// <summary>
/// GridView异步加载url地址的图片,通知CustomColumnDataEventArgs参数
/// </summary>
public class AsynDownImage
{
private string _URL { get; set; }
private Bitmap _Image { get; set; }
private bool _IsReady = false;
private object _Locker = new object();
private List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs> _listens;
private Control _owner;
public AsynDownImage(string url, Control owner)
{
_owner = owner;
_listens = new List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs>();
_URL = url;
this.StartDown();
}
void StartDown()
{
Task.Run(() =>
{
//判断图片路径是否为网络路径
if (IsUrl(this._URL))
{
try
{
//读取文件
using (WebClient wc = new WebClient())
{
Stream tream = wc.OpenRead(this._URL);
this._Image = new Bitmap(tream);
this._Image = new Bitmap(this._Image, new Size(60, 60));
}
}
catch (Exception ex)
{
this._Image = null;
}
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
}
});
}
public void AddListener(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs obj)
{
lock (_Locker)
{
_listens.Add(obj);
if (this._IsReady)
{
obj.Value = this._Image;
}
}
if (this._IsReady)
_owner.Invoke(new DownImageCallback(callback), obj);//立即刷新,显示图片
}
void RunNotify()
{
lock (_Locker)
{
foreach (var o in _listens)
{
o.Value = this._Image;
}
}
_owner.Invoke(new DownImageCallback(callback), new CustomColumnDataEventArgs(null, -1, null, false));//立即刷新,显示图片
}
private void callback(CustomColumnDataEventArgs e)
{
if (e != null && e.Column != null)
{
GridView view = (GridView)e.Column.View;
int h = view.GetRowHandle(e.ListSourceRowIndex);
if (h >= 0)
{
view.InvalidateRowCell(h, e.Column);//刷新单个图片单元格
}
else
{
view.Invalidate();
}
}
}
/// <summary>
/// 识别urlStr是否是网络路径
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsUrl(string url)
{
if (Regex.IsMatch(url, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?"))
{
return true;
}
else
{
return false;
}
}
}
public delegate void DownImageCallback(CustomColumnDataEventArgs e);
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// GridView异步加载url地址的图片,通知CustomColumnDataEventArgs参数
/// </summary>
public class AsynDownImage
{
private string _URL { get; set; }
private Bitmap _Image { get; set; }
private bool _IsReady = false;
private object _Locker = new object();
private List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs> _listens;
private Control _owner;
public AsynDownImage(string url, Control owner)
{
_owner = owner;
_listens = new List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs>();
_URL = url;
this.StartDown();
}
void StartDown()
{
Task.Run(() =>
{
//判断图片路径是否为网络路径
if (IsUrl(this._URL))
{
try
{
//读取文件
using (WebClient wc = new WebClient())
{
Stream tream = wc.OpenRead(this._URL);
this._Image = new Bitmap(tream);
this._Image = new Bitmap(this._Image, new Size(60, 60));
}
}
catch (Exception ex)
{
this._Image = null;
}
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
}
});
}
public void AddListener(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs obj)
{
lock (_Locker)
{
_listens.Add(obj);
if (this._IsReady)
{
obj.Value = this._Image;
}
}
if (this._IsReady)
_owner.Invoke(new DownImageCallback(callback), obj);//立即刷新,显示图片
}
void RunNotify()
{
lock (_Locker)
{
foreach (var o in _listens)
{
o.Value = this._Image;
}
}
_owner.Invoke(new DownImageCallback(callback), new CustomColumnDataEventArgs(null, -1, null, false));//立即刷新,显示图片
}
private void callback(CustomColumnDataEventArgs e)
{
if (e != null && e.Column != null)
{
GridView view = (GridView)e.Column.View;
int h = view.GetRowHandle(e.ListSourceRowIndex);
if (h >= 0)
{
view.InvalidateRowCell(h, e.Column);//刷新单个图片单元格
}
else
{
view.Invalidate();
}
}
}
/// <summary>
/// 识别urlStr是否是网络路径
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsUrl(string url)
{
if (Regex.IsMatch(url, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?"))
{
return true;
}
else
{
return false;
}
}
}
public delegate void DownImageCallback(CustomColumnDataEventArgs e);
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网