Adding a picture to MS Access file and retrieve it :-s

AMat

New member
Joined
May 17, 2007
Messages
1
Programming Experience
1-3
Hello Every one,

I've added a Picture to the MS Access file as an Ole object. and i don't have any clue how can i retrieve this picture from a VB.NET application. i'm not sure if i was supposed to insert the picture as an Ole object is there any other way?! what i need is to retrieve a Picture from an MS Access Database using VB.NET

Can anyone help me?!

Thank you
 
You can insert the file path instead of the object. Although links have their own issues, at least you database won't be massive in size because it's housing all the pics instead.

You need to put the links in url format, i.e. \\server1\pictures\picture1.jpg

On your form, have a picture box, and simply bind it to your picture column in your dataTable...
 
u usually don't store pictures within the db itself unless u are creating a VBA application..

do this instead

Create a folder in ur app.startup path call img
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Directory.Exists(Application.StartupPath & "\img") = False Then
            Directory.CreateDirectory(Application.StartupPath & "\img")
        End If
End Sub

o by the way be sure to imports system.io or it wont work
This is the add photo button, u open a image file, resize it to the size u want and save it into the \img folder, u can save it any format u want, but i chose .png, have a picture box to display the photo in your form

txtEmpId in this case means the photo will be referenced via the primary key of the data item
VB.NET:
Private Sub btnAddPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPhoto.Click
        Dim objOpenFileDialog As New OpenFileDialog
        picDisplay.Visible = False
        'Set the Open dialog properties
        With objOpenFileDialog
            .Filter = "Image Files|*.jpg; *.jpeg; *.png; *.bmp; *.gif|Bitmap files (*.bmp)|*.bmp|JPEG files (*.jpg; *.jpeg)|*.jpg; *.jpeg|GIF files (*gif) |*.gif|PNG files(.png)| *.png"
            .FilterIndex = 1
            .Title = "Select picture..."
        End With

        If objOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim selectedImage As Image
            Dim tmpPhoto As Bitmap
            Try
                selectedImage = Image.FromFile(objOpenFileDialog.FileName)

                tmpPhoto = New Bitmap(selectedImage, 90, 110)
                Dim g As Graphics = Graphics.FromImage(tmpPhoto)
                ' Select/change interpolation mode here.
                g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                g.DrawImage(selectedImage, 0, 0, 90, 110)
                Me.picDisplay.Image = tmpPhoto
                'TODO copy files into img folder                
	   tmpPhoto.Save(Application.StartupPath & "\img\P" & Me.txtEmpId.Text.Trim & ".png", System.Drawing.Imaging.ImageFormat.Png)
                picDisplay.Visible = True
            Catch ex As Exception
                If File.Exists(Application.StartupPath & "\img\P" & Me.txtEmpId.Text.Trim & ".png") Then
                    File.Delete(Application.StartupPath & "\img\P" & Me.txtEmpId.Text.Trim & ".png")
                    tmpPhoto.Save(Application.StartupPath & "\img\P" & Me.txtEmpId.Text.Trim & ".png", System.Drawing.Imaging.ImageFormat.Png)
                End If
            End Try
        End If

        'Clean up
        objOpenFileDialog.Dispose()
        objOpenFileDialog = Nothing
    End Sub

to display everytime u navigate call this method
VB.NET:
Private Sub loadPhoto(ByVal reset As Boolean)

        Dim strTemp As String
        Dim i As Integer

         i = CInt(Me.txtEmpId.Text.Trim) 
         strTemp = "P" & i & ".png"
  
            Try
                Dim fs As New IO.FileStream(Application.StartupPath & "\img\" & strTemp, FileMode.Open, FileAccess.Read)
                picDisplay.Image = Image.FromStream(fs)
                picDisplay.Visible = True
                fs.Close()
            Catch
                If File.Exists(Application.StartupPath & "\img\" & strTemp) = False Then
                    picDisplay.Visible = False
                End If
            End Try

    End Sub
 
Last edited:
Back
Top