Loading and Saving Annotations Using Databases (Visual Basic)

Note: This topic is for Document/Medical only.

Take the following steps to start a project and to add some code that demonstrates adding annotation objects to or deleting annotation objects 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 LEAD Raster Annotation Object Library to your project.

 

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

6.

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.

7.

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.

8.

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

9.

Declare the following variables as Global variables:

Dim RasterIO As New LEADRasterIO
Dim RasterAnn As New LEADRasterAnnotation
Dim RasterAnnToolBar As New LEADRasterAnnToolBar
Dim Recordset As New ADODB.Recordset
Dim Connection As New ADODB.Connection
Dim strConnect As String

10.

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

   LEADRasterView1.Raster.UnlockSupport L_SUPPORT_DOCUMENT, L_KEY_DOCUMENT
   RasterIO.Load LEADRasterView1.Raster, "c:\parrots.jpg", 0, 0, 1
   RasterAnn.AnnParentRasterView = Me.LEADRasterView1
   RasterAnn.AnnUserMode = ANN_USERMODE_DESIGN
   
   RasterAnnToolBar.AnnParentRasterView = LEADRasterView1
   
   RasterAnnToolBar.Visible = True
   RasterAnnToolBar.Create 0, 0, ANN_TOOLALIGN_TOP
   
   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 "

 

11.

Code the cmdAddRecord button's Click procedure as follows:

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

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

   Recordset.AddNew
   
   RasterAnn.AnnSaveArray ANN_FMT_NATIVE, False, RasterVariant, SAVE_OVERWRITE, 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

 

12.

Code the cmdMoveNext button's Click procedure as follows:

   Dim RasterVariant As New LEADRasterVariant
   Dim i As Integer
   Dim nSize As Integer
   
   If Not Recordset.EOF Then Recordset.MoveNext
   
   If (Recordset.EOF) Then
      MsgBox "Unable to get the annotation from the database"
      Recordset.MovePrevious
   Else
      nSize = Recordset.Fields("Size").Value
      RasterVariant.Type = VALUE_ARRAY_BYTE
      RasterVariant.ItemCount = nSize
      For i = 0 To nSize – 1
         RasterVariant.ShortItemValue(i) = Recordset.Fields("Image").Value(i)
      Next
      RasterAnn.AnnLoadArray RasterVariant, nSize, 1
   End If

13.

Code the cmdMovePrevious button's Click procedure as follows:

   Dim RasterVariant As New LEADRasterVariant
   Dim i As Integer
   Dim nSize As Integer
   
   If Not Recordset.BOF Then Recordset.MovePrevious
   
   If (Recordset.BOF) Then
      MsgBox "Unable to get the annotation from the database"
      Recordset.MoveNext
   Else
      nSize = Recordset.Fields("Size").Value
      RasterVariant.Type = VALUE_ARRAY_BYTE
      RasterVariant.ItemCount = nSize
      For i = 0 To nSize – 1
         RasterVariant.ShortItemValue (i) = Recordset.Fields("Image").Value(i)
      Next
      RasterAnn.AnnLoadArray RasterVariant, nSize, 1
   End If

14.

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

15.

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

16.

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

 

17.

Run your program to test it.