mercoledì 18 febbraio 2009

Recuperare un'immagine da Database con VB.Net

    Dim conta As Integer = 1
    Dim Album As New Collection
    Dim Query As String
    Dim Archivio As SqlConnection

Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
    Archivio = New SqlConnection
    Archivio.ConnectionString = "stringa di connessione"
    Query = "SELECT Foto FROM ALBUM"
    Dim cmd As New SqlCommand(Query, Archivio)
    Archivio.Open()
    Dim I As Integer = 0
    Dim reader As SqlDataReader = cmd.ExecuteReader()
    Dim Temp As Byte ()
    While reader.Read
        Temp = reader(0)
        Album.Add(Temp)
    End While
    If Album.Count > 0 Then
        Temp = CType (Album.Item(conta), Byte())
        Dim Img As MemoryStream = New MemoryStream(Temp)
        PictureBox1.Image = Image.FromStream(Img)
    End If
End Sub

Private Sub btnIndietro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIndietro.Click
    Dim Temp As Byte ()
    If conta > 1 Then
        conta = conta - 1
        Temp = CType (Album.Item(conta), Byte())
        Dim Img As MemoryStream = New MemoryStream(Temp)
        PictureBox1.Image = Image.FromStream(Img)
    End If
End Sub

Private Sub btnAvanti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvanti.Click
    Dim Temp As Byte ()
    If conta Album.Count Then
        conta = conta + 1
        Temp = CType (Album.Item(conta), Byte())
        Dim Img As MemoryStream = New MemoryStream(Temp)
        PictureBox1.Image = Image.FromStream(Img)
    End If
End Sub

Nessun commento: