Loading and Displaying an Image

Take the following steps to start a project and to add some code that loads and displays an image:

  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 "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as "Loading and Displaying an Image" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click 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 "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32 " folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Controls.WinForms.dll
    • Leadtools.Codecs.Cmp.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  6. Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and Drag and drop an instance of ImageViewer to the form. If you do not have ImageViewer in your toolbox, select Tools->Add Remove Toolbox Items from the menu. Click Browse and then select Leadtools.Controls.WinForms.DLL from "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" and then click Open and then click OK.
  7. From the toolbox (View->Toolbox), add a Button control (text = "Load").
  8. 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:

    VB
    Imports Leadtools 
    Imports Leadtools.Codecs 
    Imports Leadtools.Controls 
                     
         

    C#
    using Leadtools; 
    using Leadtools.Codecs; 
    using Leadtools.Controls; 
                     
         

  9. Add an event handler to the Form1 Load event and add the following code:

    VB
    ' the RasterCodecs object for loading/saving images 
    Private codecs As RasterCodecs 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       ' Initialize a new RasterCodecs object 
       codecs = New RasterCodecs() 
    End Sub 
                     
         

    C#
    // the RasterCodecs object for loading/saving images 
    private RasterCodecs codecs; 
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       // Initialize a new RasterCodecs object 
       codecs = new RasterCodecs(); 
    } 
                     
         

  10. Double-click the button1 Button on the form to add the event handler for it and add the following code:

    VB
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
       ' show the open file dialog 
       Dim dlg As New OpenFileDialog 
       dlg.Filter = "All Files|*.*" 
       If (dlg.ShowDialog(Me) = DialogResult.OK) Then 
          Try 
             ' try to load the file 
             Dim tempImage As RasterImage = codecs.Load(dlg.FileName) 
                              
             ' set the image into the viewer 
             RasterImageViewer1.Image = tempImage 
          Catch ex As Exception 
             MessageBox.Show(Me, ex.Message) 
          End Try 
       End If 
    End Sub 
                     
         

    C#
    private void button1_Click(object sender, System.EventArgs e) 
    { 
       // show the open file dialog 
       OpenFileDialog dlg = new OpenFileDialog(); 
       dlg.Filter = "All Files|*.*"; 
       if(dlg.ShowDialog(this) == DialogResult.OK) 
       { 
          try 
          { 
             // try to load the file 
             RasterImage tempImage = codecs.Load(dlg.FileName); 
                              
             // set the image into the viewer 
             rasterImageViewer1.Image = tempImage; 
          } 
          catch(Exception ex) 
          { 
             MessageBox.Show(this, ex.Message); 
          } 
       } 
    } 
                     
         

  11. Build, and Run the program to test it.
Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document