在DataGridView中使用鼠标点击将图像插入到图片框中 [英] Inserting Image into picturebox with mouseclick in DataGridView

查看:62
本文介绍了在DataGridView中使用鼠标点击将图像插入到图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击datagridview中的单元格时,我想在我的图片框上放置一个图像,但是当我点击一个单元格时,我的程序总是停止..



我也遇到了如何将图像转换为字节数据类型的问题..

(fyi,我在我的SQL服务器上使用varbinary(max),并且我想将它转换为字节)

有人可以帮助我吗?



I want to put an image on my picturebox when i click on a cell in my datagridview, but my program always stopped when i click on a single cell..

i'm having trouble too with how i could cast the "image" into "byte" data type..
(fyi, i use "varbinary(max)" on my SQL server, and i want to convert it to "byte")
can someone help me?

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridViewRow row;
    row = dataGridView1.CurrentRow;
    Byte[] picture = (Byte[]) row.Cells[5].Value;  //my program always stopped here
    MemoryStream ms = new MemoryStream(picture);
    pictureBox1.Image = Image.FromStream(ms);
}

推荐答案

我已经解决了,

I have solve it,,
public Image GetDataToImage(byte[] pData)
        {
            try
            {
                ImageConverter imgConverter = new ImageConverter();
                return imgConverter.ConvertFrom(pData) as Image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
        //Event Handler CellClick
        private void dgv_ProductData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            pictureBox1.Image = GetDataToImage((byte[])(dgv_ProductData.CurrentRow.Cells[5].Value));
            pictureBox2.Image = GetDataToImage((byte[])(dgv_ProductData.CurrentRow.Cells[6].Value));
        }


//双击gridview,然后textbox和picturebox从gridview获取值和图像。我的工作代码如下



public Image GetDataToImage(byte [] pData)

{

try

{

ImageConverter imgConverter = new ImageConverter();

将imgConverter.ConvertFrom(pData)作为图像返回;

}

catch(exception ex)

{

MessageBox.Show(ex.Message,Error,MessageBoxButtons.OK,MessageBoxIcon.Error );

返回null;

}

}

private void dataGridView1_CellDoubleClick(object sender,EventArgs e)

{

if(dataGridView1.SelectedCells.Count> 0)

{

int selectedrowindex = dataGridView1.SelectedCells [0] .RowIndex;



DataGridViewRow selectedRow = dataGridView1.Rows [sele ctedrowindex];



textBox1.Text = Convert.ToString(selectedRow.Cells [0] .Value);

textBox2.Text =转换.ToString(selectedRow.Cells [1] .Value);

picture_photo.Image = GetDataToImage((byte [])(selectedRow.Cells [2] .Value));

}



}
//On Double click on the gridview then textbox and picturebox get value and images from the gridview. my working code is as follows

public Image GetDataToImage(byte[] pData)
{
try
{
ImageConverter imgConverter = new ImageConverter();
return imgConverter.ConvertFrom(pData) as Image;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private void dataGridView1_CellDoubleClick(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;

DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];

textBox1.Text = Convert.ToString(selectedRow.Cells[0].Value);
textBox2.Text = Convert.ToString(selectedRow.Cells[1].Value);
picture_photo.Image = GetDataToImage((byte[])(selectedRow.Cells[2].Value));
}

}


这篇关于在DataGridView中使用鼠标点击将图像插入到图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆