在DevExpress GridControl的一列中显示图片

简介:         最近做项目的时候用到了将GridControl中一列设置为PictureEdit类型,然后通过这一列来显示图片。经过尝试发现有以下两种方式可行。方法一、知道图片的路径与名称        比如:在数据库中存储了图片的路径(包括:本地路径、服务器路径),那么在可以通过非绑定列的方式来实现。1、创建了一个非绑定列并设置其相应的属性,属性设置如下:        FieldNa

        最近做项目的时候用到了将GridControl中一列设置为PictureEdit类型,然后通过这一列来显示图片。经过尝试发现有以下两种方式可行。

方法一、知道图片的路径与名称

        比如:在数据库中存储了图片的路径(包括:本地路径、服务器路径),那么在可以通过非绑定列的方式来实现。

1、创建了一个非绑定列并设置其相应的属性,属性设置如下:
        FieldName设为 Photo(该字段名必须是唯一的)


        UnboundType设为 UnboundColumnType.Object
        ColumnEdit设为RepositoryItemPictureEdit类的实例(该操作PictureEdit 为该列的内置编辑器)


        2.、添加GridView的CustomUnboundColumnData事件,用于为非绑定列填充数据。


        既然已经设置完成了,那么具体的代码怎么编写呢?具体代码如下:

   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;
            }
        }

如果图片在单元格中显示有问题的话,可以调整  


方法二、知道图片的路径与名称

        除了方法一之外,我们还可以使用流的方式的来加载图片,即根据图片路径将图片转化为流,然后直接绑定到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];
            }
        }




相关文章
C# DEV TextEdit 设置文本框只能输入数字(整数)
C# DEV TextEdit 设置文本框只能输入数字(整数)
|
2月前
|
人工智能 物联网 API
ModelScope魔搭25年8月发布月报
🔥 这个夏天,开源热潮比气温更燃!Qwen3、GLM4.5、混元、Wan2.2、Qwen-Image等重磅模型密集发布,MoE、多模态、Agent、生图视频全爆发,ModelScope 全程 Day0 支持,生态持续进化中!
268 0
C# Dev解决gridview1_SelectionChanged和gridview1_RowCellClick事件触发两次等问题
C# Dev解决gridview1_SelectionChanged和gridview1_RowCellClick事件触发两次等问题
C# Dev解决gridview1_SelectionChanged和gridview1_RowCellClick事件触发两次等问题
|
11月前
|
安全 Linux Shell
docker运行centos提示Operation not permitted
通过上述步骤,可以有效排查和解决在Docker中运行CentOS容器时遇到的"Operation not permitted"错误。这些措施涵盖了从权限配置、安全策略到容器运行参数的各个方面,确保在不同环境和使用场景下都能顺利运行容器。如果你需要进一步优化和管理你的Docker环境
1005 3
|
设计模式 开发框架 前端开发
基于DevExpress的GridControl实现的一些界面处理功能
基于DevExpress的GridControl实现的一些界面处理功能
|
机器学习/深度学习 数据可视化 前端开发
使用Stream实现Web应用,使用YOLOv8模型对图像进行目标检测为例。
使用Stream实现Web应用,使用YOLOv8模型对图像进行目标检测为例。
348 1
|
Prometheus 监控 Cloud Native
容器化技术的性能调优与监控
【6月更文挑战第29天】本文探讨了容器(如Docker)的性能优化与监控,强调了其在云和微服务中的重要性。调优涉及资源限制设定、代码优化,通过性能测试、瓶颈分析进行迭代优化。监控目标是确保稳定性和可用性,使用工具如Portainer、CAdvisor、Prometheus来跟踪状态、性能指标和日志。监控内容涵盖容器状态、资源使用、日志和限制,策略包括设定阈值和告警机制。调优监控的优化有助于提升应用性能和企业价值。
|
机器学习/深度学习 数据采集 算法
构建高效的机器学习模型:从数据预处理到模型优化
【2月更文挑战第30天】 在机器学习的实践中,构建一个高效且准确的预测模型是每个数据科学家的核心任务。本文将深入探讨如何通过一系列策略性步骤来提升模型的性能,包括数据预处理、特征选择、模型训练以及超参数调优。我们将讨论各种技术方法,并通过实例展示这些策略是如何在实际问题中应用的。目标是为读者提供一套实用的工具和思路,以应对机器学习项目中遇到的挑战。
|
小程序 开发者
关于微信小游戏的备案,你需要的事
关于微信小游戏的备案,你需要的事
622 0