一个BindingManagerBase的问题

fwloveme 2010-07-29 01:56:45
数据库Image里有张ImageStore表,表中有一个字段ImagePro (数据类型是Image),我在前台想通过移动记录来浏览数据库里面的图片,在Form_Load里面的代码如下:
try
{
string MySQL = "Select * From ImageStore ";
SqlConnection MyConnection = new SqlConnection("Data Source=.;Initial Catalog=Image;Integrated Security=True");
MyConnection.Open();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(this.dataSet1, "ImageStore");
this.pictureBox1.DataBindings.Add(new Binding("Image", this.dataSet1, "ImageStore.ImagePro"));
//MyBind = this.BindingContext[this.dataSet1, "ImageStore"];
MyBind=this.pictureBox1.BindingContext[this.dataSet1,"ImageStore"];
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

但是运行 提示:无法将直格式化为所需类型,请问该怎么解决呢?
...全文
82 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fwloveme 2010-08-05
  • 打赏
  • 举报
回复
解决了。。。
fwloveme 2010-08-03
  • 打赏
  • 举报
回复
我等高手等的花儿都谢了。。。。。。。。。。。。
fwloveme 2010-07-30
  • 打赏
  • 举报
回复
高手们都忙啊?
fwloveme 2010-07-29
  • 打赏
  • 举报
回复
楼上的,还是提示参数无效啊,我打开数据库看了看,有三张图片,显示<Binary>啊
  • 打赏
  • 举报
回复
byte[] bs = xxx;
MemoryStream m = new MemoryStream(bs);
Bitmap bmp = new Bitmap(m);
dancingbit 2010-07-29
  • 打赏
  • 举报
回复
这段代码看不出来有什么问题,试试:
1.确认数据库中保存的图像数据与Bitmap兼容。
2.创建MemoryStream的时候断点,看看img是否为null。
fwloveme 2010-07-29
  • 打赏
  • 举报
回复
写了个转化函数,如下所示:
void PictureFormat(Object sender, ConvertEventArgs e)
{
// e.Value is the original value
Byte[] img = (Byte[])e.Value;

// Perform the conversion
// (78 is the offset to skip ONLY FOR images in NorthWind)
MemoryStream ms = new MemoryStream();
int offset = 0; // should be 0
ms.Write(img, offset, img.Length - offset);
Bitmap bmp = new Bitmap(ms);
ms.Close();

// Writes the new value back
e.Value = bmp;
}
运行提示 “参数无效”
dancingbit 2010-07-29
  • 打赏
  • 举报
回复
Binding类有个Format事件,在该事件处理方法中将数据库中提取到的数据创建为Bitmap再返回。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧