VB of the image in the database to store and display

Author:Anonymous    Updated:2008-2-25 19:10:22
Abstract Access97 to VB6 and as a development tool, introduced in the database image storage and display technologies.

Keywords database, data control, binary, image storage, image display, ADODB, Recordset

The database is the latest data management technology, is an important branch of computer science, computer information systems and modern computer application and the basis of the core. In the rapid development of science and technology today, everywhere in the information resources, the ubiquitous use of the various departments has become an important asset when engaged in the development of the procedures it is particularly important for officers.

Today, not only the operation of the database to meet the characters and figures in a single operation, image storage and display has become particularly important. Below the author will be VB6.0 and Access97 as a development tool, introduced two image display and storage methods.

Data controls and use of data binding controls

Use this method, do not write or write a small amount of code can be constructed on a simple database applications, this method was easy to accept beginners. In the example, first data binding function briefly elaborate, with all the attributes DataSource control of the data are sensitive, and they can be used directly through the Data Control database data. For example CheckBox Control, ComboBox Comtrol, TextBox Comtrol, PictureBox Control, Image Comtrol… involved in this way because the knowledge point less, relatively easy to understand, not for illustration, it is directly on programming steps.

1, from the database showed that the required picture

First, add a Data Control, set its DatabaseName and RecordSource attributes,

StrPath = App.Path
If Right (strPath, 1) <> "\" Then
StrPath strPath & = "\"
MyData.DatabaseName strPath & = "ExampleDB.mdb" 'Address inventory data
MyData.RecordSource = "Info" 'table

The second step, add the Image control used to display images, and set its DataSource DataField attribute. For example, in this case: Image1.DataSource = "MyData" and Image1.DataField = "MyPhoto." Then set the other functions of a data binding controls to be used to show other content, after two-step operation, operational procedures can show you the data.

2, in addition to the database need to store the picture

First, by using data controls with the AddNew attributes, add a button, double-click Add the following code MyData.Recordset.AddNew

The second step, Image Control picture designated picture path Image1.Picture = LoadPicture ( "picture path"), after the two-step operation, the database can be added to the picture.

The most simple and quick method, the code of rarely post. But this method in the speed and flexibility with certain restrictions, suitable for beginners and some simple applications, in order to display images flexible, the methods described below may be better suited to your needs.

Prepared by the use of the code to store and display pictures

For this method, a method, a large quantity of code, but its operational flexibility to meet the diverse forms of operation, as more programmers of all ages. But concerns relative to the more knowledge, not only to master the operation of the database, but also writing binary files read further understanding. Binary files on the database and the basic operation of the many reference books are described in more detail, please refer to be necessary. In this part of the program before the variables used as follows:

Dim RS As New ADODB.Recordset
Dim Chunk () As Byte
Const ChunkSize As Integer = 2384
Dim DataFile As Integer, Chunks, Fragment As Integer
Dim MediaTemp As String
Dim lngOffset, lngTotalSize As Long
Dim i As Integer

1, from the database showed that the required picture

First open the first database to see if a View the contents, and they continue to carry out, did not withdraw from the

RS.Source = "select * from Info Where Name = '" & sparaName &"';"
RS.ActiveConnection = "UID =; PWD =; DSN = TestDB;"
RS.Open
If RS.EOF Then RS.cCose: Exit Sub

The second step, long binary data read out data that picture, put it into the image file, process, as follows

MediaTemp strPath & = "picturetemp.tmp"
DataFile = 1
Open MediaTemp For Binary Access Write As DataFile
LngTotalSize = RS! MyPhoto.ActualSize
Chunks lngTotalSize = \ ChunkSize
Fragment = lngTotalSize Mod ChunkSize
ReDim Chunk (Fragment)
Chunk () = RS! MyPhoto.GetChunk (Fragment)
Put DataFile, Chunk ()
For i = 1 To Chunks
ReDim Chunk (ChunkSize)
Chunk () = RS! MyPhoto.GetChunk (ChunkSize)
Put DataFile, Chunk ()
Next i
Close DataFile

The third step, closing the database so that it could show the pictures to be.

RS.Close
If MediaTemp = "" Then Exit Sub
Picture1.Picture = LoadPicture (MediaTemp)
If Picture1.Picture = 0 Then Exit Subj

2, in addition to the database need to store the picture

Adding to the database storage is the image displayed picture inverse process, as long as the master of the picture shows that the operation of the storage picture of the operation will be solved, the following steps will be described below.

First open the first step in the database, the process is as follows:

RS.Source = "select * from Info;"
RS.CursorType = adOpenKeyset
RS.LockType = adLockOptimistic
RS.ActiveConnection = "UID =; PWD =; DSN = TestDB;"
RS.Open

The second step, to be converted into pictures stored in the database binary long file, as follows operation process

RS.AddNew
DataFile = 1
Open strPathPicture For Binary Access Read As DataFile
FileLen = LOF (DataFile) 'length in the document data
If FileLen = 0 Then: Close DataFile: RS.Close: Exit Sub
Chunks FileLen = \ ChunkSize
Fragment = FileLen Mod ChunkSize
ReDim Chunk (Fragment)
Get DataFile, Chunk ()
RS! MyPhoto.AppendChunk Chunk ()
ReDim Chunk (ChunkSize)
For i = 1 To Chunks
Get DataFile, Chunk ()
RS! MyPhoto.AppendChunk Chunk ()
Next i
Close DataFile

The third step is to update records, closed database, completed the picture data to the database storage.

RS.Update
RS.Close
Set RS = Nothing

In the use of both methods have their own strong points, and readers can make their own conditions for a reasonable choice.
Previous:Address Book do with VB
Next:VB6.0 beginners 10 programming tips
User Reviews
Site Search
Related Articles
Recommended article
AD