Save and display pictures with stream objects

xiaoxiao2021-03-06  114

Open VB6 and create a new project.

Add two buttons, an image control

Note: The PHOTO field type in Access is an OLE object.

The PHOTO field type in SQLServer is image

.

'** Reference Microsoft ActiveX Data Objects 2.5 Library and above

Stream objects below 'version 2.5 below

DIM iconcstr as string

DIM iconc as adodb.connection

'Save files to the database

SUB S_SAVEFILE ()

DIM ISTM As Adodb.Stream

DIM IRE As Adodb.Recordset

DIM iconcstr as string

'Read files to content

Set ISTM = New Adodb.Stream

With ISTM

.Type = adtypebinary 'binary mode

.Open

.Loadfromfile app.path "/test.jpg"

End with

'Table of the saved file

SET IRE = New Adodb.Recordset

With IRE

.Open "Select * from img", ICONC, 1, 3

.Addnew 'added a record

.Fields ("photo") = ISTM.READ

.Update

End with

'Close the object after completing

IRE.Close

ISTM.CLOSE

End Sub

Sub S_ReadFile ()

DIM ISTM As Adodb.Stream

DIM IRE As Adodb.Recordset

'Open the table

SET IRE = New Adodb.Recordset

'Get the latest record

Ire.open "SELECT TOP 1 * from img order by id desc", iconc, adopenkeyset, AdlockReadOnly

'Save to the file

Set ISTM = New Adodb.Stream

With ISTM

.Mode = AdmodeReadwrite

.Type = adtypebinary

.Open

.Write IRE ("photo")

'Note here, if there is Test1.jpg in the current directory, a file write failed error will be reported.

.Savetofile app.path & "/test1.jpg"

End with

Image1.picture = loadingPicture (app.path & "/test1.jpg")

'Close object

IRE.Close

ISTM.CLOSE

End Sub

Private submmand1_click ()

Call S_ReadFile

End Sub

Private sub fascist2_click ()

Call s_savefile

End Sub

Private sub flow_load ()

'Database connection string

Iconcstr = "provider = microsoft.jet.Oledb.4.0; persist security info = false" & _

"; Data Source = f: / 9cbs_vb / database / save image / access picture /img.mdb"

'The following statement is to connect the SQLServer database.

'Iconcstr = "provider = SQLOLEDB.1; PERSIST security info = true;" & _' "user ID = sa; password =; initial catalog = test; data source = yang"

Set iconc = new adoDB.Connection

Iconc.open iconcstr

End Sub

Private Sub Form_Unload (Cancel AS Integer)

iconc.close

Set iconc = Nothing

End Sub

You can also use binary methods to read, but you can see the simplicity of stream objects.

You can refer to the Knowledge Base article of MS to strengthen this knowledge point:

Http://support.microsoft.com/default.aspx?scid=kb;n-us;258038

转载请注明原文地址:https://www.9cbs.com/read-125641.html

New Post(0)