在picturebox里面如何绑定dataset中的图形字段?
数据集已经读出,其他字段已经绑定,请问如何将图形字段绑定到picturebox控件上?
我存储在图形字段里面的不光有bmp格式的图像还有jpg和gif的,在读取的时候显示如下错误: "GDI+ 中发生一般性错误。 "
代码如下:
Dim imageBinding As Binding
imageBinding = New Binding( "image ", ds.Tables( "person "), "照片 ")
AddHandler imageBinding.Format, AddressOf imageBinding_Format
PictureBox1.DataBindings.Add(imageBinding)
Private Sub imageBinding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs)
Dim bytes() As Byte
bytes = CType(e.Value, Byte())
Dim ms As New IO.MemoryStream()
ms.Write(bytes, 0, bytes.Length)
e.Value = Image.FromStream(ms)
End Sub
——请用vb.net说明,3x
[解决办法]
1、直接设立pic绑定列属性就ok
2、读出来的直接二进制转换文件给他就ok
你的代码怎么混着用
[解决办法]
//把图片读到pictureBox,再写入数据库
SqlConnection conn=new SqlConnection(@ "data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf ");
conn.Open();
SqlCommand cmd=new SqlCommand( "insert into fuser values ( '1a ', '1b ',@i) ",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@ "D:\windows temp\temp\1.jpg ",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add( "@i ",SqlDbType.Image,(int)fs.Length);
cmd.Parameters[ "@i "].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();
-------------------------------------------------------
//从数据库读图片到picturebox
SqlConnection conn=new SqlConnection(@ "data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf ");
conn.Open();
SqlCommand cmd=new SqlCommand( "select 照片 from fuser where password= '1b ' ",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;