【问题标题】:DataGridView - Click on Image in Cell and show new Form with full size imageDataGridView - 单击单元格中的图像并显示具有全尺寸图像的新表单
【发布时间】:2018-01-22 13:49:41
【问题描述】:

全部: 我是一个新手,使用通过 Visual Studio 数据源函数绑定到 SQL Server 数据库表的 DataGridView1。 我已经用表中的所有列填充了 DataGrid,包括一个包含 Blobbed 图像的列,该图像在 DataGridView 中正确显示为图像的一部分。 我希望用户能够单击包含图像切片的 DataGridView1 单元格并启动一个以特定图像作为背景的新表单(或至少在同一个表单上填充一个图片框),但是我似乎无法引用单击的单元格中的特定图像... 我尝试过使用“DataGridViewImageColumn”,但未成功:

    imageColumn = new DataGridViewImageColumn();
    imageColumn.Image = this.imageDataGridViewImageColumn.Image;
    frmPic.BackgroundImage = imageColumn.Image;

有人可以建议使用正确的代码来引用此特定图像吗?

【问题讨论】:

    标签: c# image datagridview


    【解决方案1】:

    我认为你可以使用 dataGridView_CellClick 事件处理程序,

    所以你可以这样做:

     private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
     { 
        // set index to be the pic column, assume that pic in column 0
        int index = 0;
    
        // just check that the clicked cell is the right cell
        // and every thing is okay.
    
        if (dataGridView.CurrentCell.ColumnIndex.Equals(index) && e.RowIndex != -1)
        if (dataGridView.CurrentCell != null && dataGridView.CurrentCell.Value != null)
        {
            // cast to image
            Bitmap img = (Bitmap)dataGridView.CurrentCell.Value;
    
            // load image data in memory stream
            MemoryStream ms = new MemoryStream();
            img.save(ms, ImageFormat.Jpeg);
    
            // now you can open image in any picBox by memory stream.
            // if you want to open pic in other form, you need to pass memory 
            // stream to this form, e.g., in constructor.
    
            // open other form
            NewForm obj = new NewForm(ms)
        }
     }
    

    NewForm Constructor中使用内存流加载图片框中的图片。

     public NewForm(MemoryStream ms)
     {
        picBox.Image = Image.FromStream(ms);
     } 
    

    希望对你有帮助,祝你好运!

    【讨论】:

    • 感谢 Essam,但以下行正在生成未处理的异常: Bitmap img = (Bitmap)dataGridView.CurrentCell.Value; “无法将“System Byte[]”类型的对象转换为 System.Drawing.Bitmap 类型的对象”你能帮忙吗?
    • 没关系 - 我想通了 - 我用过:ImageConverter ic = new Image Converter(); Image img = (Image)ic.ConvertFrom(dataGridView1.CurrentCell.Value);
    • 感谢您的领先!
    猜你喜欢
    • 2017-04-23
    • 1970-01-01
    • 2015-12-14
    • 2016-01-26
    • 2010-10-02
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多