如何在picturebox vb.net中显示图像 [英] How to show image in picturebox vb.net
问题描述
Dim cmd < span class =code-keyword> As 新 SqlCommand( 从FamilyDetails中选择MemPhoto,其中FamilyID = @ FamilyID,sqlcon)
cmd.Parameters.Add( @ FamilyID,SqlDbType.Int).Value = txtsearchbox.Text
Dim dt 作为 新 DataTable
Dim adp As 新 SqlDataAdapter(cmd)
adp.Fill(dt)
我的表格上有七个图片框。但我很困惑如何将数据绑定到图片框...
请帮助.....
Hi
首先根据选择查询从sql下载并将该图片保存在临时文件夹中,并在图片框中找到图片位置url你给那个带有图片名称的临时文件夹路径。
< pre lang =vb> PictureBox1.ImageLocation = C:\inetpub \wwwroot \ ccp \ temp \ a.ico
我认为你将你的图片以二进制格式存储在sql中。
Dim cn As New SqlConnection()
cn.ConnectionString = 数据源=。; uid = sa; pwd = wintellect; database = master
cn.Open()
Dim sql 作为 字符串 = 从student中选择* rollno = + comboBox1.SelectedItem
Dim cmd As 新 SqlCommand(sql,cn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows 然后
dr.Read()
Dim 数据作为 字节()= DirectCast (博士( MemPhoto),字节( ))
Dim ms 作为 新 MemoryStream(数据)
pictureBox1.Image = Image.FromStream(ms)
结束 如果
cn.Close()
你有修改连接字符串并根据你的要求选择查询
------------------------------------------ -----------------------------
Dim cmd 作为 新 SqlCommand ( 从FamilyDetails中选择MemPhoto,其中FamilyID = @ FamilyID,sqlcon)
cmd.Parameters .Add( @ FamilyID,SqlDbType.Int).Value = txtsearchbox.Text
Dim dt 作为 新 DataTable
Dim adp As 新 SqlDataAdapter(cmd)
adp.Fill(dt)
如果 dt.Rows( 0 )( )。ToString IsNot 没什么 然后 ' 查看行(0)
Dim data1 As Byte ()= DirectCast (dt.Rows( 0 )( ),Byte ()) ),字节())
Dim ms As 新 MemoryStream(data1)
PictureBox1.Image = Image.FromStream(ms)
ms.Close()
结束 如果
如果 dt.Rows( 1 )( )。ToString IsNot 没什么 然后 ' 查看行(1)
Dim data2 作为 字节()= DirectCast (dt.Rows( 0 )(
Dim ms As 新 MemoryStream(data2)
PictureBox2.Image = Image .FromStream(ms)
ms.Close()
结束 如果
如果 dt.Rows( 2 )( )。ToString IsNot 没什么 然后 ' 见行(2)
Dim data3 作为 字节 ()= DirectCast (dt.Rows( 0 )( ), Byte ())
Dim ms As 新 MemoryStream(data3)
PictureBox3.Image = Image.FromStream(ms)
ms.Close()
End 如果
............................... .................最多七个picturebox
在上面的代码中像硬编码,我的意思是,我们知道7张照片和7张图片框,所以没有循环我将使用dt.Rows(0)()到PictureBox1,dt.Rows(1)(在每个图片框中绑定照片)至PictureBox2等等。至少你通过循环改变这部分。
Hai I have little confusion. I am developing a family card program for my church. I want to show the photos of all members of a family in a form. please help me what I do...
Dim cmd As New SqlCommand("Select MemPhoto from FamilyDetails where FamilyID=@FamilyID", sqlcon)
cmd.Parameters.Add("@FamilyID", SqlDbType.Int).Value = txtsearchbox.Text
Dim dt As New DataTable
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dt)
There are seven picture boxes on my form. But I am little confused how to bind data to picture boxes...
please help.....
Hi First download and save that picture in temp folder from sql based on select query and in picture box imagelocation url u give that temp folder path with picture name.
PictureBox1.ImageLocation = "C:\inetpub\wwwroot\ccp\temp\a.ico"
I think u store ur picture stored in binary format in sql.
Dim cn As New SqlConnection() cn.ConnectionString = "Data Source=.;uid=sa;pwd=wintellect;database=master" cn.Open() Dim sql As String = "Select * from student where rollno=" + comboBox1.SelectedItem Dim cmd As New SqlCommand(sql, cn) Dim dr As SqlDataReader = cmd.ExecuteReader() If dr.HasRows Then dr.Read() Dim data As Byte() = DirectCast(dr("MemPhoto"), Byte()) Dim ms As New MemoryStream(data) pictureBox1.Image = Image.FromStream(ms) End If cn.Close()
U have modify connection string and select query based on ur requirements
-----------------------------------------------------------------------
Dim cmd As New SqlCommand("Select MemPhoto from FamilyDetails where FamilyID=@FamilyID", sqlcon) cmd.Parameters.Add("@FamilyID", SqlDbType.Int).Value = txtsearchbox.Text Dim dt As New DataTable Dim adp As New SqlDataAdapter(cmd) adp.Fill(dt) If dt.Rows(0)("").ToString IsNot Nothing Then 'See Rows(0) Dim data1 As Byte() = DirectCast(dt.Rows(0)(""), Byte()) Dim ms As New MemoryStream(data1) PictureBox1.Image = Image.FromStream(ms) ms.Close() End If If dt.Rows(1)("").ToString IsNot Nothing Then 'See Rows(1) Dim data2 As Byte() = DirectCast(dt.Rows(0)(""), Byte()) Dim ms As New MemoryStream(data2) PictureBox2.Image = Image.FromStream(ms) ms.Close() End If If dt.Rows(2)("").ToString IsNot Nothing Then 'See Rows(2) Dim data3 As Byte() = DirectCast(dt.Rows(0)(""), Byte()) Dim ms As New MemoryStream(data3) PictureBox3.Image = Image.FromStream(ms) ms.Close() End If ................................................upto seven picturebox
In above code like hard coded,i mean, we know 7 photo and 7 picturebox in form,so without looping i will bind photo in each picturebox using dt.Rows(0)("") to PictureBox1,dt.Rows(1)("") to PictureBox2 and so on.so at lest u change this part by looping .
这篇关于如何在picturebox vb.net中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!