C# Dev GridView表格的CustomUnboundColumnData事件异步加载图片类AsynDownImage
C# Dev GridView表格的CustomUnboundColumnData事件异步加载图片类AsynDownImage
扫一扫加微信
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 bool _UseLocalImage = true;//使用本地缓存图片
public AsynDownImage(string url)
{
_UseLocalImage = true;
_URL = url;
_listens = new List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs>();
this.StartDown();
}
/// <summary>
/// 开始加载图片
/// </summary>
void StartDown()
{
Task.Run(() =>
{
//判断本地缓存文件
if (_UseLocalImage && ImageFileCache.Instance.Exists(ImageBuckleName.small_images, this._URL))
{
this._Image = ImageFileCache.Instance.GetImage(ImageBuckleName.small_images, this._URL);
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
return;
}
//判断图片路径是否为网络路径
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));//缩放图片
//缓存图片
if (_UseLocalImage)
ImageFileCache.Instance.AddImage(this._URL, this._Image, ImageBuckleName.small_images);
}
}
catch (Exception ex)
{
this._Image = null;
}
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
return;
}
});
}
public void AddListener(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs obj)
{
lock (_Locker)
{
_listens.Add(obj);
if (this._IsReady)
{
obj.Value = this._Image;
}
}
}
void RunNotify()
{
lock (_Locker)
{
foreach (var o in _listens)
{
o.Value = this._Image;
}
}
}
/// <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;
}
}
}
//来源: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 bool _UseLocalImage = true;//使用本地缓存图片
public AsynDownImage(string url)
{
_UseLocalImage = true;
_URL = url;
_listens = new List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs>();
this.StartDown();
}
/// <summary>
/// 开始加载图片
/// </summary>
void StartDown()
{
Task.Run(() =>
{
//判断本地缓存文件
if (_UseLocalImage && ImageFileCache.Instance.Exists(ImageBuckleName.small_images, this._URL))
{
this._Image = ImageFileCache.Instance.GetImage(ImageBuckleName.small_images, this._URL);
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
return;
}
//判断图片路径是否为网络路径
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));//缩放图片
//缓存图片
if (_UseLocalImage)
ImageFileCache.Instance.AddImage(this._URL, this._Image, ImageBuckleName.small_images);
}
}
catch (Exception ex)
{
this._Image = null;
}
lock (_Locker)
{
_IsReady = true;
}
RunNotify();
return;
}
});
}
public void AddListener(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs obj)
{
lock (_Locker)
{
_listens.Add(obj);
if (this._IsReady)
{
obj.Value = this._Image;
}
}
}
void RunNotify()
{
lock (_Locker)
{
foreach (var o in _listens)
{
o.Value = this._Image;
}
}
}
/// <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;
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
ImageFileCache 类:
C# 本地图片缓存器ImageFileCache (www.csframework.com)
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网