从数据库读出图片显示到datagridview
数据库里面就一个字段Image 类型image 存放着图片的信息
怎么取出来放到datagridview 的picturebox 里面??? 或者直接在datagridview显示
数据库里面就一个字段Image 类型image 存放着图片的信息
怎么取出来放到datagridview 的picturebox 里面??? 或者直接在datagridview显示
调试欢乐多
string st = "select BookImage from Books";
DataTable dt = DB.Select(st);
byte[] imgbt = (byte[])dt.Rows[0][0];
if (imgbt.Length != 0)
{
MemoryStream imgms = new MemoryStream(imgbt);
Bitmap images = new Bitmap(imgms); //这里提示参数类型不正确
Column1.Image = images; //Column1为我picturebox的 字段
} DB连接数据库的类。 帮忙一下
{
if (dataGridview1.Columns[e.ColumnIndex].Name.Equals("Image"))
{
string path = e.Value.ToString();
e.Value = GetImage(path);
}
}
public System.Drawing.Image GetImage(string path)
{
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
System.Drawing.Image result = System.Drawing.Image.FromStream(fs); fs.Close(); return result; }
使用DataGridViewImageColumn
DataGridViewImageColumn column = new DataGridViewImageColumn();
dataGridView1.Columns.Add(column);
column.HeaderText = "图片";
column.Image = System.Drawing.Image.FromFile("路径"); ((DataGridViewImageCell)this.dataGridView1.Rows[e.RowIndex].Cells[ "图片列"]).Value = "路经 "
dataGridView1.Columns["uwork"].Visible = false; //隐藏该列
DataGridViewImageColumn colphoto = new DataGridViewImageColumn();
colphoto.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; //调整列宽,使其足够显示出标题
colphoto.HeaderText = "图片";
colphoto.Name = "photo";
//一下为添加计算字段函数
dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
dataGridView1.Columns.Add(colphoto);关于计算字段的函数请参考:
http://www.cnblogs.com/liminzhang/articles/592955.html
{
MemoryStream imgms = new MemoryStream(imgbt,true);
imgms .Write(imgbt, 0, imgbt.Length); //这里提示参数类型不正确
Column1.Image = new Bitmap(imgms);//Column1为我picturebox的 字段 imgms.Close();
}
byte[] imgbt = (byte[])dt.Rows[0][0];
是不是这句出问题啦。。
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection("Data Source=10.192.35.20;User ID=sa;password=sa123;initial catalog=ems;Integrated Security=no;");
cmd.CommandText = "select top 1 * from Tbl_Test_Img";
cmd.CommandType = CommandType.Text;
if (cmd.Connection.State != ConnectionState.Open)
{
cmd.Connection.Open();
}
SqlDataReader dr = cmd.ExecuteReader(); dr.Read();
MemoryStream ms = new MemoryStream((byte[])dr["img"]);
Image img = Image.FromStream(ms);
pictureBox1.Image = img;