locked
Loading a jpg file into a PictureBox directly from an Access DataBase field in VB2010

    Question

  • This is the first time ever I've used a Forum so please forgive me if I don't follow the correct protocol or format.

    I am trying to 1) write code in VB2010 that will get a JPG image from a Access Database field and write the image to a PictureBox 2) write code which will write a JPG file to an Access Database field. I have already sucessfully coded this in VB6, without fully understanding what I was doing.

    Browsing the various forum's I have come across a few discussions on this subject, but due to my complete amerture status I don't seem to be able to follow the logic.

    Below is the VB6 code, can somebody help me convert this into VB2010 code.

    Writing JPG file to DataBase:

    Dim bytBlob() As Byte
    Dim intNum As Integer

    intNum = FreeFile
    TextBoxPhoto.Text = "C:\Photos\Dog.jpg"
    Open TextBoxPhoto.Text For Binary As #intNum
    ReDim bytBlob(FileLen(TextBoxPhoto.Text))
    Get #intNum, , bytBlob
    Close #intNum
    Recordset.Fields("fldPhoto").AppendChunk bytBlob

    Photo from DataBase to PictureBox

    'Load images - Blob method
    strTempPic = "C\Temp\TempPic.jpg"
    If Len(Dir(strTempPic)) > 0 Then Kill strTempPic
    intFile = FreeFile
    Open strTempPic For Binary As #intFile
    lngImageSize = Recordset.Fields("fldPhoto").ActualSize
    bytChunk() = Recordset.Fields("fldPhoto").GetChunk(lngImageSize)
    Put #intFile, , bytChunk()
    Close #intFile
    Image1.Picture = LoadPicture(strTempPic)
    Kill strTempPic

    Naturally, if there is a more elegant and simpler way of doing this, I'm All ears

    ThanX b- your help is much appreciated

    Thursday, November 24, 2011 11:44 AM

Answers

  • Hi Kevin,

    Welcome to the MSDN Forum.

    I didn't convert your code to VB.net, but here is a code snippet about this topic, please take a look at it:

        Private Function SavePicture(ByVal picName As String) As Boolean
            Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=d:\db\Database11.accdb")
            cn.Open()
            Dim bitP() As Byte = System.IO.File.ReadAllBytes(picName)
            Dim Command As New OleDbCommand("insert into pic(pic) values(@img)", cn)
            Command.Parameters.Add("@img", OleDbType.Binary)
            Command.Parameters("@img").Value = bitP
    
            Try
                Command.ExecuteNonQuery()
            Catch ex As Exception
                Return False
            Finally
                cn.Close()
            End Try
    
            Return True
        End Function
    
        Private Function LoadPicture() As Image
            Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=d:\db\Database11.accdb")
            cn.Open()
    
            Dim Command As New OleDbCommand("SELECT * FROM pic", cn)
            Dim Reader As OleDbDataReader = Command.ExecuteReader()
            Dim picture As Image = Nothing
    
            While Reader.Read
                Dim pictureData As Byte() = CType(Reader.GetValue(1), Byte())
                Using stream As New IO.MemoryStream(pictureData)
                    picture = Image.FromStream(stream)
                End Using
            End While
            cn.Close()
    
            Return picture
    
        End Function
    

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, November 28, 2011 9:32 AM