|Saving a picture in a database|
|By: Richard Paweska Date: 25/04/2000|
This tip describes how to save a small picture (less than 65 KB) into a database field.
First thing, be sure to have a table whose records include a
field named "Picture", of the LongBinary type. You will also
need a hidden picture box, named pctHidden (set Visible
property to False), and a Data
control to deal with the database.
Use the following code in Visual Basic:
|Dim strTmpPath As String
Dim strChar As String
Dim strBitmap As String
'Temporary path to save image
strTmpPath = "c:\~tmpimg"
'Clear any previous contents from the string
'that the bitmap will be saved to
strBitmap = ""
'Clear the picture box
Set pctHidden.Picture = Nothing
'Load the image to the picture box
Set pctHidden.Picture = LoadPicture("picturefilepath")
'Delete temporary file if it already exists
If Dir(strTmpPath) <> "" Then Kill strTmpPath
'Save the contents of the picture box to the path defined above
SavePicture pctHidden.Picture, strTmpPath
'Open the saved picture to read into a string
Open strTmpPath For Binary Access Read As #1
'Initialize the string
strChar = String(LOF(1) + 1, " ")
'Get the contents of the file and place them into a string
Do While Not EOF(1)
Get #1, , strChar
strBitmap = strBitmap & CStr(strChar)
'Close the open bitmap file
'Initialize the Data control
.DatabaseName = DBName
.RecordSource = "tablename"
'Add a new record
.Recordset!Picture = strBitmap
Replace "picturefilepath" with the path of the image to save; you can also specify a picture from one of the controls in your project. Also replace "tablename" and datDataControl with proper values.
|Visual Basic Programming Zone is a website by
Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it