On a past few days, I’m working on Visual Basic Project on a friend of mine and one of the functionality is that the users are able to add and update their profile picture, on a first glance I didn’t even think that I can do this in VB but after few minutes of digging I found myself doing this is very simple and I want to share how easy this snippet to you.
The aim of the project is to be able the users to add, edit and update user profile with their favorite avatar, like what we have in most social sites now.
CommonDialog Components and into the form add and rename the name of the following tools
- CommonDialog = commondialog
- Image = imgProfile
- Command Button = cmdSelectFile
- Text Box = txtimgPath
- Command Button = cmdUpload
Okay lets add our code, double click on
cmdUpload button to view code window and add this code.
Private Sub cmdSelectFile_Click()
imgProfile.Stretch = True
imgProfile.Picture = LoadPicture(commondialog.FileName)
txtimgPath.Text = commondialog.FileName
Private Sub cmdUpload _Click()
'This will contains codes to save your data to the database
!imgPath = txtimgPath.Text 'imgPath is a table field name
So, now as expected when you click on Select File Button it will pop up another window to select which image you want to upload.
Now it’s time to retrieve our uploaded image, to do that just simply copy this code below.
'Put this code into your search function or sub
imgProfile.Picture = LoadPicture(!imgPath)
You can now create user profile with Avatar as easy as that, share your idea below, actually there are still lots of way to upload images but I found this one a very handy to use.
Happy coding ^_^