Hi all,
I have created a program in Visual Basic 6 that will be used in every department (IT Department, Accounting etc.) in our school. It will save all the basic information of a person like ID_number, Name, Address, Contact info etc. It's a simple program that handles membership or registration of every person that is enrolled on a certain Department.
But I am having a problem saving images that is very important in a membership program. I also want that only a file extension of .jpg picture is allowed for saving the images. Can anyone help me about this? I am using DAO to connect to database and Microsoft Access 2003 as my backend.
Thanks.
Saving .jpg images to database
Saving image using VB6 is very easy. But we will not save those images directly to the database because it will make the program heavier. What we are going to do is to save all images in one location and only the path of the image will be saved in your database.
Every time you are going to save the profile of a person, as for the image, you will only get the name of the picture and its file extension and then save it to your database. On retrieving of picture, you will just call the complete path name of the picture which you have already saved in the database and using the image control you will set its source to be able to display the image, the source will be the image path. For much clearer explanation, I will show you the codes for saving images.
Put the following codes and controls on your form:
-
1 Image control, Width = 2400, Height = 2400.
-
1 Command Button for browsing images to your computer.
-
1 Common Dialog.
On “General Declarations” put these codes:
Dim pic_name As String, pic_ext As String, pic_changed As Boolean
Double click the command button and paste these codes:
CommonDialog1.Filter = " Picture Files|*.jpg|JPEG Images(*.jpg)".
cdb.ShowOpen
If CommonDialog1.FileName <> "" Then
Image1.Picture = LoadPicture(cdb.FileName)
pic_name = CommonDialog1.FileName
pic_ext = Right(CommonDialog1.FileTitle, 4)
pic_changed = True
End If
Paste these codes after end sub:
Public Sub pic()
If Form1. CommonDialog1.FileName <> "" Then
Form1.Image1.Picture = LoadPicture(Form1. CommonDialog1.FileName)
pic_name = Form1. CommonDialog1.FileName
pic_ext = Right(Form1. CommonDialog1.FileTitle, 4)
pic_changed = True
End If
End Sub
Replace “Form1” with the name of your form.
Add these codes to your save command:
If pic_name <> "" Then
FileCopy pic_name, App.Path & "Pictures" & !ID_number & pic_ext
!Picture = "Pictures" & !Id_no & pic_ext
End If
Replace !Picture with the corresponding field name in your database. The name of the saved image will be in this format ID number of the person + Lastname + pic extension. I’ve included the file extension to be able to view the picture when you open the target folder. That’s it!
Answered By
gigarose
0 points
N/A
#126034
Saving .jpg images to database
Saving the image in MS Access database is not hard but the question is:
1. Where is the image come from (WEB camera or other capturer Image device)?
And i am sure that you don't want your database to become very when the database is more than 1 GB, and if that is not the concern then i teach you how to save the image to your database using VB6 and MS Access 2003 Database.
1. Add a field on your existing table
FieldName |
Data Type |
Photo |
OLE Object |
2. In your Existing Form (In VB6) like where the Information of your Member is or where you want to put the Picture: Add Image Object and Name it ImgPic.
3. Add CommondDialog name it CDImage.
4. Add Command Button name it cmdBrowse.
Put this on the code of cmdbrowse:
Private Sub cmdBrowse_Click()
Me.cDImage.ShowOpen'
If Me.CDImage.FileName & "" <> "" Then
Set Me.imgPic.Picture = LoadPicture(Me.CDImage.FileName)
End If
End Sub
5. Then save the image on your database just add this to the existing form you have.
Dim ImageByte() As Byte
Dim PathImage As String
Dim fileint As Integer
SavePicture Me.imgPic.Picture, App.Path
PhotoImage = App.Path
ReDim ImageByte(FileLen(PathImage))
fileint = FreeFile()
Open PhotoImage For Binary As #fileint
Get #fileint, , ImageByte
Close fileint
'(Open your database on using recordset, see below example)
rst.Open "Membership", cn, adOpenStatic, adLockOptimistic, adCmdTable
with rst
.addnew
Field(Photo).appendchunk imageByte ' the name of the field in the database is PHOTO
.update
End with
rst.close