检索图片框中的图像 [英] retrieve images in picture box
本文介绍了检索图片框中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我通过带有Image列的dgv时,我有一个小的isssure,我需要在dgv旁边的图片框中更改图像
我在form_load上有这个代码和dgID_textchanged
I have a small isssure when I am going thru dgv with Image column and I need to change images in picture box beside dgv
I have this code on form_load and dgID_textchanged
if (image1.Image != null)
image1.Image.Dispose();
SqlCommand cmd22 = new SqlCommand("Image_proc", cs);
cmd22.CommandType = CommandType.StoredProcedure;
cmd22.Parameters.Add("@imgId", SqlDbType.Int).Value =
Convert.ToInt32(dgID.Text);
SqlDataAdapter adp = new SqlDataAdapter(cmd22);
DataTable dt = new DataTable();
try
{
if (cs.State == ConnectionState.Closed)
cs.Open();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["Image"]);
image1.Image = Bitmap.FromStream(ms);
image1.Refresh();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (cs.State == ConnectionState.Open)
cs.Close();
}
当dgv的单元格中有图像时它一切都很好但是它是null值然后我得到错误无法将类型'System.DBNull'的对象强制转换为'System.Byte ...而且我被卡住了
代码块添加 - OriginalGriff [/ edit]
it all works great when there is a image in the cell of dgv but whene it is null value then I get Error Unable to cast object of type 'System.DBNull' to type 'System.Byte ... and I am stuck
[edit]Code block added - OriginalGriff[/edit]
推荐答案
简单 - 检查它。
Simple - check it.
if (dt.Rows.Count > 0)
{
object o = dt.Rows[0]["Image"];
Image im = null;
if (o != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])o);
im = Bitmap.FromStream(ms);
}
image1.Image = im;
image1.Refresh();
}
这篇关于检索图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文