Visual Std 2010

DB에서 저장된 이미지 PictureBox로 불러오기

소마후니 2015. 9. 14. 18:30

Imports System.Data.OleDb
Imports System.IO
Public Class Form1


    Dim conn As New OleDbConnection
    Dim DA As OleDbDataAdapter
    Dim DS As New DataSet
    Dim counter As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

        conn.ConnectionString = "Provider=SQLOLEDB;Data Source=DCFDL380G7;Persist Security Info=True;Password=daechang1@;User ID=yuth;Initial Catalog=DCF_TECH"

        DA = New OleDbDataAdapter("Select image from image", conn)

        DA.Fill(DS)

        counter = 0

        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

        If counter < Me.DS.Tables(0).Rows.Count - 1 Then

            counter += 1

        End If
    End Sub
    Private Function GetImageFromByteArray(ByVal picData As Byte()) As Image
        If picData Is Nothing Then
            Return Nothing
        End If ' is this is an embedded object?
        Dim bmData As Integer = IIf((picData(0) = 21 Or picData(1) = 28), 78, 0)

        ' load the picture
        Dim img As Image = Nothing
        Try
            Dim ms As New MemoryStream(picData, bmData, picData.Length - bmData)
            img = Image.FromStream(MS)
        Catch
        End Try ' return what we got Return img

    End Function
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

        If counter > 0 Then

            counter -= 1

 

        End If
    End Sub

End Class