How upload an image for a record.

Posted on 2012-08-10
Medium Priority
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
  • 3
  • 2
LVL 40
ID: 38283170
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 28

Expert Comment

ID: 38286153
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
ID: 38286689
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 28

Expert Comment

ID: 38286830
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 28

Accepted Solution

Ark earned 2000 total points
ID: 38286921
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

ID: 38290789

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 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