How upload an image for a record.

Posted on 2012-08-10
Last Modified: 2012-08-13
I have an VB.Net windows application. I am storing the data in the fields for employees.
Now i want to store the images of the employees.
The pics will be stored in folder with file name saved as id as in employees tables,
What data type should i take for storing images.
1, can be varchar where in we can map the path
2, what could be other way.

Please send example.
Question by:searchsanjaysharma
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    If the files are all in the same directory and have the same name as the user ID, you do not have to store anything, you already have the information you need, because the ID is already in the database.

    When you need to retrieve the image in your application, simply go to the directory that holds the images, and get the employee image from the file that has the same name as the ID field.
    LVL 27

    Expert Comment

    If photos aren't huge, I would suggest store them directly in the table in the Image (or binary - depends on DB type) type field. With this approach records and images will always be synchronized - when you delete employer his image will be deleted too, while with file data approach you'll have orphan images and/or records
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    If there is an application that is used to manage the employees, a single line of code is sufficient to delete the photo when an employee is deleted, so storing the images separately is usually not a problem.

    Manipulating photos in files is a lot easier than doing so in a database.

    Updating the photos with newer ones would also be easier since it could be done without having an application to do so.
    LVL 27

    Expert Comment

    a single line of code is sufficient to delete the photo when an employee is deleted
    How many lines is sufficient if file was assidently deleted/renamed?
    Manipulating photos in files is a lot easier than doing so in a database. Updating the photos with newer ones would also be easier since it could  be done without having an application to do so
    Agree - with 100 employes. What about some thousands?
    LVL 27

    Accepted Solution

    PS. I've made  a similar app with now up to 60K+ records. And first approach was storing photos in files. Beleive me - it was nightmare! Every day personal accidently copied new images over old files for different users. Further, while DB grows, we need to use filenames like John_Smith_1.jpg, John_Smith_2.jpg, John_Smith_3.jpg etc. Finally, I replaced filenames with userID, but in this case
    Updating the photos with newer ones would also be easier
    became impossible. Finally I switched to DB storing - it's much easy than sounds:
    1. Create 1 more field in tblUsers table (Name = Photo, AllowNulls=Yes, DataType=image)
    2. Add a picture box on form (pbPhoto)
    3. Bind picture box to datasource:
    pbPhoto.DataBindings.Add("Image", bsUsers, "Photo", True, DataSourceUpdateMode.Never, My.Resources.anonymous)

    Open in new window

    pbPhoto - picturebox,
    "Image" - property name,
    bsUsers - bindingsource
    My.Resources.anonymous - default image for empty (DBNull) records
    I prefer update data source manually, so I use DataSourceUpdateMode.Never
    4. Add context menu to picture box. I use 4 choices - Load from file, Photo from WebCam, From scanner and Delete
    For Load - use standard OpenFileDialog:
        Private Sub LoadPhoto()
            Dim sPath As String
            Dim w, h As Integer
            Dim ratio As Double
            With ofdLoadPhoto
                .FileName = ""
                .Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
                .Title = "Select photo"
                .RestoreDirectory = True
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    sPath = .FileName
                    If sPath <> "" Then
                        Dim img As New Bitmap(sPath)
                        Dim rf As System.Drawing.Imaging.ImageFormat = img.RawFormat
                        w = img.Width
                        h = img.Height
                        ratio = Math.Max(w / pbPhoto.Width, h / pbPhoto.Height)
                        If ratio > 1 Then 'zoom image to fit picturebox
                            w = CInt(w / ratio)
                            h = CInt(h / ratio)
                            Dim imgResized As New Bitmap(img, w, h)
                            Dim g As Graphics = Graphics.FromImage(imgResized)
                            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                            g.DrawImage(img, 0, 0, w, h)
                            img = imgResized
                        End If
                        pbPhoto.Image = img
                        pbPhoto.DataBindings("Image").WriteValue() 'Update data source
                    End If
                End If
            End With
        End Sub

    Open in new window

    For web cam and scanner - see attached file (sorry, MsgBoxes are in Russian :))
    For Delete: bsUsers.Current("Photo")=DBNull.Value

    Thats All :)

    Author Closing Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now