图片框绑定 [英] PictureBoxBinding

查看:58
本文介绍了图片框绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个带有图片框的Windows窗体,并且我有一个使用表适配器链接到该窗体的访问数据库.该数据库具有OLE对象类型的字段,并存储位图图像.
我需要能够从数据库检索图像到picturebox控件,但是我的代码显示错误-参数无效.请帮助我

Hi I have a windows form with a picturebox and i have an access database that is linked to the form with a table adapter. The database has a field that is of type OLE Object and stores bitmap images.
I need to be able to retrieve the image from the database to the picturebox control but the code i have is presenting an error- Parameter is not valid. Please Help Me

//Method
public Bitmap GetPhoto(byte[] photoBits)
{
  if (photoBits != null && photoBits.Length > 0)
  {
    using (MemoryStream stream = new MemoryStream(photoBits))
    {
      return new Bitmap(stream);
    }
  }
  else
  {
    return null;
  }
}
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select M_Photo from MemberPhoto where M_id=''" + mSearch.Text + ''";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);

推荐答案

如果您指出了代码的哪一部分给出了错误,将会很有帮助.

但是,假设这是最后一行,那么就会想到一两个问题.

1.如果da.Fill()不返回任何行,则将得到一个错误,尽管不是. 2.由于您仅检索1列,因此可以尝试
It would help if you indicated which part of your code is giving the error.

However, assuming it is the the last line, there are one or two things that spring to mind.

1. if the da.Fill() returns no rows you would get an error, though not the one you are getting.
2. Since you are only retrieving 1 column you could try
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0][0]);


3.您确定(byte[])dd.Tables[0]强制转换正常吗?


3. Are you sure that the (byte[])dd.Tables[0] cast is working?


这篇关于图片框绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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