在DevExpress GridControl的一列中显示图片

该做法在滑动过快的情况下 可能会出现卡死

 设置colum properties属性
1.FieldName 设定命名(该字符串必须唯一)
2.ColumnEdit 设置PirtureEdit
3.UnboundType 设置Object

 

 

在MainView 的数据表中添加事件 columnUnboundColumnData

图片在单元格中显示调整设置

 repository-pictureEdit 【SizeMode】

复制代码
 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;
        }
    
    }
复制代码

设置图像方式:

 clip:裁剪
zoom:适应剧中
Stretch:拉伸填充
Squeeze:挤压
StretchVertical:垂直拉伸
StretchHorizontal:横纵拉伸

posted @   一招致命九虎山  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
点击右上角即可分享
微信分享提示