作者: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的一列中显示图片的更多相关文章

  1. DevExpress GridControl 列中显示图片

    一.GridControl 的Columns中添加列 1.列名:FieldName命名为img 2.类型:ColumnEdit属性中 选择PictureEdit类型(RepositoryItemPic ...

  2. GridControl 列中显示图片 z

    如何在 DevExpress.XtraGrid.GridControl 显示图片列. 方法很多,我把它们逐一写在附言中,方便大家分情况合理使用. 附言1  附言2  附言3  第 1 条附言  ·  ...

  3. Devexpress GridView 列中显示图片

    首先将图片添加到ImageList中 添加GridView中Column void gridView1_CustomUnboundColumnData(object sender, DevExpres ...

  4. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  5. 我的Android进阶之旅------&gt; Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...

  6. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

  7. 在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片

    很多时候,在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片.可以通过以下方式: <img src="xxx.jpg" onError="th ...

  8. 在博客中显示图片_Mac版

    主要是防止自己忘掉 为了解决一开始自己想在写入的博客中添加本地图片,直接链接的话在自己的电脑倒是可以显示图片,但是在别人的电脑上就没办法加载图片了,问各路大神也没人愿意解答,百度也没有想要的答案,只好 ...

  9. Java中显示图片的方法

    最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...

随机推荐

  1. JAVA 学习笔记 - 反射机制

    1.   JAVA反射机制的概念 2. 怎样实例化一个 Class对象 Class.forName(包名.类名); 对象.getClass(); 类.class; ================== ...

  2. 浅谈CSS中的定位知识

    1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...

  3. Swift3命名空间的实现

    最近在看一些Swift开源库的时候,发现了一些优秀的开源库都使用了命名空间,例如Kingfisher这个开源库中,就针对UIImage,UIImageView,UIButton做了命名空间的扩展.通过 ...

  4. CREATE DATABASE - 创建新数据库

    SYNOPSIS CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ LOCATION [=] 'dbpath' ] [ TEMPLATE ...

  5. oracle分析函数之ratio_to_report

    ratio_to_report主要完成对百分比的计算,语法为ratio_to_report(exp) over()也就是根据over窗口函数的作用区间,求出作用区间中的单个值在整个区间的总值的比重比如 ...

  6. svn in xcode5

    两种办法,一是使用比较成熟的svn客户端,二是使用终端.以下为终端方法: 假设已经通过Xcode->Preferences->Accounts将repository: http://mys ...

  7. Xshell连接Centos7.5和yum

    目 录 第1章 Centos7 IP地址的配置    1 1.1 第一种配置ip方法(nmtui)    1 1.2 第二种 修改网卡配置文件    5 1.2.1 使用cat查看配置文件    5 ...

  8. Python中接收用户的输入

    一.如何去接收用户的输入?使用函数 input() 函数 input() 让程序暂停运行,等待用户输入一些文本,获取用户的输入后,Python将其存储到一个变量中,以方便后期使用. name = in ...

  9. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

  10. CSS实现折叠面板

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...