在DevExpress GridControl的一列中显示图片
作者:jiankunking 出处:http://blog.csdn.net/jiankunking
近期做项目的时候用到了将GridControl中一列设置为PictureEdit类型,然后通过这一列来显示图片。经过尝试发现有下面两种方式可行。
方法一、知道图片的路径与名称
比方:在数据库中存储了图片的路径(包含:本地路径、server路径),那么在能够通过非绑定列的方式来实现。
1、创建了一个非绑定列并设置其对应的属性。属性设置例如以下:
FieldName设为 Photo(该字段名必须是唯一的)
UnboundType设为 UnboundColumnType.Object
ColumnEdit设为RepositoryItemPictureEdit类的实例(该操作PictureEdit 为该列的内置编辑器)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmt1bmtpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
2.、加入GridView的CustomUnboundColumnData事件。用于为非绑定列填充数据。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmt1bmtpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
既然已经设置完毕了。那么详细的代码怎么编写呢?详细代码例如以下:
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "Photo" && e.IsGetData)
{
//RefImage是存储图片路径的那一列
string filePath = (string)((DataRowView)e.Row)["RefImage"];
Image img = null;
try
{
//推断图片路径是否为网络路径
if (UrlDiscern(filePath))
{
//文件是否存在
if (RemoteFileExists(filePath))
{
//读取文件
using (WebClient wc = new WebClient())
{
img = new Bitmap(wc.OpenRead(filePath));
}
}
}
// 推断本地文件是否存在
else if (LocalFileExists(filePath))
{
//载入本地图片
img = Image.FromFile(filePath);
}
//pictureEdit列绑定图片
e.Value = img;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
/// <summary>
/// 推断远程文件是否存在
/// </summary>
/// <param name="fileUrl"></param>
/// <returns></returns>
public bool RemoteFileExists(string fileUrl)
{
HttpWebRequest re = null;
HttpWebResponse res = null;
try
{
re = (HttpWebRequest)WebRequest.Create(fileUrl);
res = (HttpWebResponse)re.GetResponse();
if (res.ContentLength != 0)
{
//MessageBox.Show("文件存在");
return true;
}
}
catch (Exception)
{
//MessageBox.Show("无此文件");
return false;
}
finally
{
if (re != null)
{
re.Abort();//销毁关闭连接
}
if (res != null)
{
res.Close();//销毁关闭响应
}
}
return false;
}
/// <summary>
/// 推断本地文件是否存在
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool LocalFileExists(string filePath)
{
if (File.Exists(filePath))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 识别urlStr是否是网络路径
/// </summary>
/// <param name="urlStr"></param>
/// <returns></returns>
public bool UrlDiscern(string urlStr)
{
if (Regex.IsMatch(urlStr, @"((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;
}
}
假设图片在单元格中显示有问题的话,能够调整
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmt1bmtpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
方法二、知道图片的路径与名称
除了方法一之外,我们还能够使用流的方式的来载入图片,即依据图片路径将图片转化为流。然后直接绑定到RepositoryItemPictureEdit列上就可以。
此时不须要改动列的绑定类型。仅仅须要该列的FieldName与数据源中的byte[]流的所在列的名称一致就可以,
假设这么绑定无效的话,能够在gridcontrol的数据源(此处假设为Dataset)中新增一列
ds.Tables[0].Columns.Add("Photo", System.Type.GetType("System.Byte[]"));
然后,依据路径载入图片到Photo列中,
<pre name="code" class="html"> byte[] bb = PubFunc.getImageByte(path, webClient);
ds.Tables[0].Rows[i]["Photo"] = bb;
当中,可能会用到的函数例如以下:
/// <summary>
/// 返回图片的字节流byte[]
/// </summary>
/// <param name="imagePath"></param>
/// <param name="webClient"></param>
/// <returns></returns>
public byte[] getImageByte(string imagePath)
{
byte[] imgByte = null;
try
{
if (UrlDiscern(imagePath))
{
using(WebClient webClient=new WebClient())
{
Bitmap bt = new Bitmap(webClient.OpenRead(imagePath));
imgByte = PubFunc.ImgToByte(bt);
}
}
else
{
using (FileStream files = new FileStream(imagePath, FileMode.Open))
{
imgByte = new byte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
return imgByte;
}
/// <summary>
/// 图片转换成字节流
/// </summary>
/// <param name="img">要转换的Image对象</param>
/// <returns>转换后返回的字节流</returns>
public byte[] ImgToByte(Image img)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
byte[] imagedata = null;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imagedata = ms.GetBuffer();
return imagedata;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
return null;
}
}
小注:
使用以上方法,高速滑动滑动条的时候,会出现卡死的现象,由于上述代码是每次实时读取图片资源的。应该加入一个图片路径、图片 字典,以降低图片的反复读取。
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "Photo" && e.IsGetData)
{
string filePath = (string)((DataRowView)e.Row)["RefImage"];
if (!images.ContainsKey(filePath))
{
Image img = null;
try
{
if (PubFunc.UrlDiscern(filePath))
{
if (FileUpDownload.RemoteFileExists(filePath))
{
using (WebClient wc = new WebClient())
{
//Bitmap bmtemp = new Bitmap(wc.OpenRead(filePath));
//img = new Bitmap(bmtemp, 75, 75);
img = new Bitmap(wc.OpenRead(filePath));
}
}
}
else if (PubFunc.LocalFileExists(filePath))
{
img = Image.FromFile(filePath);
}
images.Add(filePath, img);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
e.Value = images[filePath];
}
}
在DevExpress GridControl的一列中显示图片的更多相关文章
- DevExpress GridControl 列中显示图片
一.GridControl 的Columns中添加列 1.列名:FieldName命名为img 2.类型:ColumnEdit属性中 选择PictureEdit类型(RepositoryItemPic ...
- GridControl 列中显示图片 z
如何在 DevExpress.XtraGrid.GridControl 显示图片列. 方法很多,我把它们逐一写在附言中,方便大家分情况合理使用. 附言1 附言2 附言3 第 1 条附言 · ...
- Devexpress GridView 列中显示图片
首先将图片添加到ImageList中 添加GridView中Column void gridView1_CustomUnboundColumnData(object sender, DevExpres ...
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
- 在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片
很多时候,在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片.可以通过以下方式: <img src="xxx.jpg" onError="th ...
- 在博客中显示图片_Mac版
主要是防止自己忘掉 为了解决一开始自己想在写入的博客中添加本地图片,直接链接的话在自己的电脑倒是可以显示图片,但是在别人的电脑上就没办法加载图片了,问各路大神也没人愿意解答,百度也没有想要的答案,只好 ...
- Java中显示图片的方法
最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...
随机推荐
- Android(java)学习笔记202:JNI之hello.c(c代码功能实现)指针语法解析
1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...
- intellij idea集成github
IDEA配置github并上传项目 http://www.cnblogs.com/jinjiyese153/p/6796668.html github ssl验证 https://www.cnblog ...
- Java格式化CST时间(mysql date类型)
在从mysql导入数据时候,mysql里的日期是格林威治时间,普通格式化不行,这里总结一下格式化格林威治时间的方法: Date date = new Date(); System.out.printl ...
- BZOJ 3158 千钧一发 最小割
分析: 偶数对满足条件2,所有奇数对满足条件1. 如果你能一眼看出这个规律,这道题就完成了一半. 我们只需要将数分为两类,a值为奇数,就从S向这个点连容量为b值的边,a值为偶数,就从这个点向T连容量为 ...
- prop 和 attr 中一些羞羞的事情
引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...
- vim使用学习
1.1在正常模式下,使用h,j,k,l实现左,下,上,右移动. (如果不再正常模式下,使用Esc键进入正常模式) 1.2退出vim,先进入到正常模式,输入:q!退出,但不保存任何修改. 1.3在正常模 ...
- 2017 计蒜之道 初赛 第一场 B阿里天池的新任务(简单)
题链:"https://nanti.jisuanke.com/t/15500" 本来希望通过找循环节然后套KMP来通过后面题的,可是只过了B题,可能循环节不一定是存在的. #inc ...
- I2C详细介绍
I2C时序 1.开始和停止: 说明: 开始:在SCL的高电平的时候SDA线的从高电平到低电平的跳变定义为开始 停止:在SCL的高电平的时候SDA线的从低电平到高电平的跳变定义为停止 2.有效数据的位置 ...
- 确定协议-通过分析系统阶段需要知道该系统能不能进行性能测试-Omnipeek
- jquery对JSON字符串的解析--eval函数
jquery eval解析JSON中的注意点介绍----https://www.jb51.net/article/40842.htm