Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Add Data Binding to RasterImageViewer Control
Take the following steps to create and run a program that shows how to add data binding to a RasterImageViewer control. Note: You should first create a database file using MS Access that has at least one table, and the table name should be "Example". This table should include at least one file using the "Image" name and the "OLE Object" data type.
  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.
  4. Type the project name as "DataBindingTutorial" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    • Leadtools.WinForms.CommonDialogs.File.dll
  6. Click Select and then clickOK to add the above DLLs to the application.
  7. Drag an OleDbDataAdapter control from the Data group in the Toolbox and drop it on Form1. When the Data Adapter Configuration Wizard dialog appears, perform the following steps:
    • Click New Connection..., and the Add Connection dialog will appear.
    • Click Change..., and the Change Data Source dialog will appear.
    • From Data source list select Microsoft Access Database File and click OK.
    • In the Add Connection dialog click Browse....and browse to the place where you created the database file and select the file. Then clickOpen and in the Add Connection dialog click OK.
    • In the Data Adapter Configuration Wizard dialog click Next. When the message box appears, click No.
    • Select Use SQL statments and click Next.
    • Click Query Build... and then click Next.
    • In the Add Table dialog on the Tablestab select Example.and click Add and then click Close.
    • In the Query Builder dialog from Example check * (All Columns) item then click OK.
    • In the Data Adapter Configuration Wizard dialog click Next and then click Finish. Notice that the oleDbConnection1 control has been created.
    • In the design view right-click on oleDbDataAdapter1 and click on Generate DataSet.. item.
    • In Generate Dataset dialog click OK. Noticee that a dataSet11 icon has been created.
  8. Drag and drop a BindingSource control onto Form1 from the Data group. Now change the following properties of bindingSource1:
    PropertyValue
    DataSource dataSet11
    DataMember Example
  9. Drag and drop a BindingNavigator control onto Form1 from the Data group. Now change the following properties of bindingNavigator1:
    PropertyValue
    BindingSource bindingSource1
    DataMember Example
  10. Drag a Panel control from the Containers group in the Toolbox and drop it onto Form1. Now change the following properties of panel1:
    PropertyValue
    Name _pnlControls
    Dock Left
  11. Drag and drop a button in _pnlControls. Now change the following properties of button1:
    PropertyValue
    Name _btnLoad
    Text Load
  12. Drag and drop a button in _pnlControls. Now change the following properties of button2:
    PropertyValue
    Name _btnSubmit
    Text Submit
  13. Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    [Visual Basic]

     
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms
    Imports Leadtools.WinForms.CommonDialogs.File
    
    [C#]
     
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;
    using Leadtools.WinForms.CommonDialogs.File;
    
  14. Add the following private variables to the Form1 class:

    [Visual Basic]

    
    private _viewer As RasterImageViewer
    private _codecs As RasterCodecs
    
    [C#]
    
    private RasterImageViewer _viewer;
    private RasterCodecs _codecs;
    
  15. Add an event handler to the Form1 Load event and add the following code:

    [Visual Basic]

    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
      oleDbDataAdapter1.Fill(dataSet11)
    
      ' Initialize a new RasterCodecs object
      RasterCodecs.Startup()
      _codecs = New RasterCodecs()
    
      _viewer = New RasterImageViewer()
      _viewer.BackColor = Color.Bisque
      _viewer.Dock = DockStyle.Fill
      Controls.Add(_viewer)
      _viewer.BringToFront()
      _viewer.BindingRasterCodecs = _codecs
      _viewer.DataBindings.Add(New System.Windows.Forms.Binding("BindingData", bindingSource1, "Image", True))
    End Sub   
    
    [C#]
    
    private void Form1_Load(object sender, EventArgs e)
    {
       oleDbDataAdapter1.Fill(dataSet11);
    
       // Initialize a new RasterCodecs object
       RasterCodecs.Startup();
       _codecs = new RasterCodecs();
       
       _viewer = new RasterImageViewer();
       _viewer.BackColor = Color.Bisque;
       _viewer.Dock = DockStyle.Fill;
       Controls.Add(_viewer);
       _viewer.BringToFront();
       _viewer.BindingRasterCodecs = _codecs;
       _viewer.DataBindings.Add(new System.Windows.Forms.Binding("BindingData",
                                                                 bindingSource1,
                                                                 "Image",
                                                                 true));
    }
    
  16. Add the following code to the Form1 class:

    [Visual Basic]

    
    Private Sub _btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs)
     Dim ofd As RasterOpenDialog = New RasterOpenDialog(_codecs)
    
     ofd.DereferenceLinks = True
     ofd.CheckFileExists = False
     ofd.CheckPathExists = True
     ofd.EnableSizing = False
     ofd.LoadFileImage = False
     ofd.LoadOptions = False
     ofd.LoadRotated = False
     ofd.LoadCompressed = False
     ofd.Multiselect = False
     ofd.ShowGeneralOptions = False
     ofd.ShowLoadCompressed = False
     ofd.ShowLoadOptions = False
     ofd.ShowLoadRotated = False
     ofd.ShowMultipage = False
     ofd.ShowPreview = True
     ofd.ShowProgressive = False
     ofd.ShowRasterOptions = False
     ofd.ShowTotalPages = False
     ofd.ShowDeletePage = False
     ofd.ShowFileInformation = True
     ofd.UseFileStamptoPreview = False
     ofd.PreviewWindowVisible = True
     ofd.Title = "LEADTOOLS Open Dialog"
    
     If ofd.ShowDialog(Me) = DialogResult.OK Then
     _viewer.Image = _codecs.Load(ofd.OpenedFileData(0).Name, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
     End If
    End Sub   
    
    [C#]
    
    private void _btnLoad_Click(object sender, EventArgs e)
    {
      RasterOpenDialog ofd = new RasterOpenDialog(_codecs);
    
      ofd.DereferenceLinks = true;
      ofd.CheckFileExists = false;
      ofd.CheckPathExists = true;
      ofd.EnableSizing = false;
      ofd.LoadFileImage = false;
      ofd.LoadOptions = false;
      ofd.LoadRotated = false;
      ofd.LoadCompressed = false;
      ofd.Multiselect = false;
      ofd.ShowGeneralOptions = false;
      ofd.ShowLoadCompressed = false;
      ofd.ShowLoadOptions = false;
      ofd.ShowLoadRotated = false;
      ofd.ShowMultipage = false;
      ofd.ShowPreview = true;
      ofd.ShowProgressive = false;
      ofd.ShowRasterOptions = false;
      ofd.ShowTotalPages = false;
      ofd.ShowDeletePage = false;
      ofd.ShowFileInformation = true;
      ofd.UseFileStamptoPreview = false;
      ofd.PreviewWindowVisible = true;
      ofd.Title = "LEADTOOLS Open Dialog";
    
      if (ofd.ShowDialog(this) == DialogResult.OK)
      {
         _viewer.Image = _codecs.Load(ofd.OpenedFileData[0].Name,
                                      0,
                                      CodecsLoadByteOrder.BgrOrGray,
                                      1,
                                      1);
      }
    }
    
  17. Add the following code to the Form1 class:

    [Visual Basic]

    
    Private Sub _btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
     bindingSource1.EndEdit()
     oleDbDataAdapter1.Update(dataSet11)
    End Sub   
    
    [C#]
    
    private void _btnSubmit_Click(object sender, EventArgs e)
    {
      bindingSource1.EndEdit();
      oleDbDataAdapter1.Update(dataSet11);
    }
    
  18. Build, and Run the program to test it.
  19. Save this project to use for testing other code samples.