在VB.NET中将PictureBox绑定到DataColum

时间:2013-10-14 15:05:37

标签: vb.net datatable picturebox

我有一个包含PictureBox的数据转发器,需要从网络服务器获取图片。我已经有一个下载图像的功能,但绑定似乎没有工作。我对使用Windows窗体很新,我不太确定我做错了什么。

我尝试过使用gridview,我可以确认它确实显示了图像。

Public Function DlImg(ByVal _URL As String) As Byte()
    Dim _tmpImage As Image = Nothing
    Dim BytesOut As Byte()
    Try
        Dim _HttpWebRequest As System.Net.HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)
        _HttpWebRequest.AllowWriteStreamBuffering = True
        Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse():
        Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()
        _tmpImage = Image.FromStream(_WebStream)
        _WebResponse.Close()
        Using picture As Image = _tmpImage
            Using stream As New IO.MemoryStream
                picture.Save(stream, Imaging.ImageFormat.Jpeg)
                BytesOut = stream.GetBuffer()
            End Using
        End Using
    Catch _Exception As Exception
        Return Nothing
    End Try

    Return BytesOut
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListData.Columns.Add("Image", Type.GetType("System.Byte[]"))
    For Each row As DataRow In ListData.Rows
        row("Image") = DlImg("http://rental.joshblease.co.uk/propertyimages/p184js94kv1qco1e1e1jt71ouv11lm6.jpg")
    Next row
    ImgListItem.DataBindings.Add("Image", ListData, "Image", True)
    DataGridView1.DataSource = ListData
    DataRepeater1.DataSource = ListData
End Sub

0 个答案:

没有答案