Loading and Saving Images Using Databases (Visual Basic)

Take the following steps to start a project and to add some code that demonstrates adding images to or deleting images from the BLOB field of a database file.

You need to create a database file (DB1.MDB) with the fields listed below, before running this tutorial.

Field Name

Type

Image

OleObject

Size

Number

 

1.

Start Visual Basic.

2.

Add the LEAD RasterView Control to your project.

 

On the Project pull-down menu, use the Components option, and select the LEAD RasterView Control (14.5).

3.

Add the LEAD Raster Object Library and LEAD Raster Variant Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD Raster Object Library (14.5).

 

On the Project pull-down menu, use the References option, and select the LEAD Raster Variant Object Library (14.5).

4.

Add the LEAD RasterIO Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD RasterIO Object Library (14.5).

5.

Add the Microsoft ActiveX Data Object 2.6 Library to your project.

 

On the Project pull-down menu, use the References option, and select the Microsoft ActiveX Data Object 2.6 Library.

6.

Select the LEAD RasterView control; then add the control to your main form. Size and position the control as you want it to appear at run time.

7.

Add six command buttons to your form and name them as follows:

 

Name

Caption

 

cmdAddRecord

Add Record

 

cmdMoveNext

Move Next

 

cmdMovePrevious

Move Previous

 

cmdDeleteRecord

Delete Record

 

cmdConnect

Connect to Database

 

cmdDisconnect

Disconnect From Database

8.

Declare the following variables as Global variables:

Dim RasterIO As New LEADRasterIO
Dim Recordset As New ADODB.Recordset
Dim Connection As New ADODB.Connection
Dim strConnect As String

9.

Add the following initialization code to the main form's Load procedure.

   RasterIO.Load LEADRasterView1.Raster, "c:\parrots.jpg", 0, 0, 1
   
   cmdMoveNext.Enabled = False
   cmdMovePrevious.Enabled = False
   cmdAddRecord.Enabled = False
   cmdDeleteRecord.Enabled = False
   
   strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\DB1.mdb"

 

10.

Code the cmdAddRecord button's Click procedure as follows:

   Dim ByteArray() As Byte
   Dim RasterVariant As New LEADRasterVariant
   Dim i As Long

   If Connection.State = adStateClosed Then
      MsgBox "Connection closed", vbInformation, "Save error"
      Exit Sub
   End If

   Recordset.AddNew
   
   Set RasterVariant = RasterIO.SaveArray(LEADRasterView1.Raster, FILE_BMP, 0, 0)

   ReDim ByteArray(0 To RasterVariant.ItemCount - 1) As Byte

   For i = 0 To RasterVariant.ItemCount – 1
      ByteArray(i) = RasterVariant.ShortItemValue(i)
   Next I

   Recordset.Fields("Image").Value = ByteArray
   Recordset.Fields("Size").Value = RasterVariant.ItemCount
   
   If Not Recordset.BOF Then Recordset.MoveFirst
   
   Recordset.Update
   
   If Recordset.RecordCount > 0 Then
    cmdMoveNext.Enabled = True
    cmdMovePrevious.Enabled = True
    cmdDeleteRecord.Enabled = True
   End If

11.

Code the cmdMoveNext button's Click procedure as follows:

   Dim RasterVariant As New LEADRasterVariant
   Dim i As Long
   Dim nSize As Long
   Dim Data As Variant
   
   If Not Recordset.EOF Then Recordset.MoveNext
   
   If (Recordset.EOF) Then
      MsgBox "Unable to get the Image from db"
      Recordset.MovePrevious
   Else
      nSize = Recordset.Fields("Size").Value
      Data = Recordset.Fields("Image").Value
      RasterVariant.Type = VALUE_ARRAY_BYTE
      RasterVariant.ItemCount = nSize
      For i = 0 To nSize – 1
         RasterVariant.ShortItemValue(i) = Data(i)
      Next I
      
      RasterIO.LoadArray LEADRasterView1.Raster, RasterVariant, 0, 1, -1, nSize
   End If

12.

Code the cmdMovePrevious button's Click procedure as follows:

   Dim RasterVariant As New LEADRasterVariant
   Dim i As Long
   Dim nSize As Long
   Dim Data As Variant
   
   If Not Recordset.BOF Then Recordset.MovePrevious
   
   If (Recordset.BOF) Then
      MsgBox "Unable to get the Image from db"
      Recordset.MoveNext
   Else
      nSize = Recordset.Fields("Size").Value
      RasterVariant.Type = VALUE_ARRAY_BYTE
      RasterVariant.ItemCount = nSize
      Data = Recordset.Fields("Image").Value
      For i = 0 To nSize – 1
         RasterVariant.ShortItemValue(i) = Data(i)
      Next
      
      RasterIO.LoadArray LEADRasterView1.Raster, RasterVariant, 0, 1, -1, nSize
   End If

13.

Code the cmdDeleteRecord button's Click procedure as follows:

   If Connection.State = adStateClosed Then
      MsgBox "Connection closed", vbInformation, "Delete error"
      Exit Sub
   End If
   If Not Recordset.BOF Or Not Recordset.EOF Then
      Recordset.MoveLast
      Recordset.Delete
      Recordset.Update
      
      If Recordset.RecordCount <= 0 Then
         cmdMoveNext.Enabled = False
         cmdMovePrevious.Enabled = False
         cmdDeleteRecord.Enabled = False
      End If
   End If

14.

Code the cmdConnect button's Click procedure as follows:

      If Connection.State = adStateClosed Then
         Connection.Open strConnect
      End If
   
      If Recordset.State = adStateOpen Then Recordset.Close
      Set Recordset.ActiveConnection = Connection
      Recordset.Open "SELECT * FROM Images", Connection, adOpenKeyset, adLockOptimistic

      If Not Recordset.BOF Then Recordset.MoveFirst
   
      If Not Recordset.EOF Then
         cmdMoveNext.Enabled = True
         cmdMovePrevious.Enabled = True
         cmdDeleteRecord.Enabled = True
      End If
      
      cmdAddRecord.Enabled = True

 

15.

Code the cmdDisconnect button's Click procedure as follows:

      If Connection.State = adStateOpen Then Connection.Close
      If Recordset.State = adStateOpen Then Recordset.Close
      cmdMoveNext.Enabled = False
      cmdMovePrevious.Enabled = False
      cmdAddRecord.Enabled = False
      cmdDeleteRecord.Enabled = False

 

16.

Run your program to test it.