Loading and displaying an image in Silverlight

Take the following steps to start a project and to add some code that will load and display an image into the LEADTOOLS Silverlight Control.

  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, select "Silverlight" sub-type, and choose "Silverlight Application" in the Templates List. Note: You may need to select to use the .NET Framework 3.5.
  4. Type the project name as "Load And Display" 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 proceeding dialog, uncheck the "Host the Silverlight application in a new Web site" option.
  6. In the "Solution Explorer" window, right-click on the "References" folder for the Silverlight project and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, browse to the "<LEADTOOLS_INSTALLDIR>\Bin\Silverlight" or "<LEADTOOLS_INSTALLDIR>\Bin\Silverlight4" folder (depending on your target Silverlight version), and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Bmp.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Gif.dll
    • Leadtools.Codecs.J2k.dll
    • Leadtools.Codecs.Png.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Dicom.dll
    • Leadtools.Windows.Controls.dll

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

  7. Open the MainPage.xaml file and copy the below XAML code into the editor:

    [XAML]

    <UserControl x:Class="Load_And_Display.MainPage"   
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
       xmlns:Leadtools_Windows_Controls="clr-namespace:Leadtools.Windows.Controls;assembly=Leadtools.Windows.Controls"   
       Width="800" Height="800"> 
       <Grid x:Name="LayoutRoot" Background="White"> 
          <StackPanel Orientation="Vertical" > 
             <Leadtools_Windows_Controls:RasterImageViewer x:Name="viewerControl" SizeMode="Normal"  Width="500" Height="500"></Leadtools_Windows_Controls:RasterImageViewer> 
             <Button Name="myButton" Content="Load" FontSize="30" Click="Button_Click"></Button> 
          </StackPanel> 
       </Grid> 
    </UserControl> 

  8. Switch to MainPage.xaml code view (right-click Page.xaml in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    VB
    Imports System.IO 
    Imports Leadtools 
    Imports Leadtools.Codecs 
                     
         

    C#
                      
    using System.IO; 
    using Leadtools; 
    using Leadtools.Codecs; 
                     
         

  9. Add the following class function:

    VB
    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
      Try   
      Dim ofd As OpenFileDialog = New OpenFileDialog() 
      If ofd.ShowDialog() = True Then 
         Dim fileStream As FileStream = ofd.File.OpenRead() 
         Try 
            Dim codecs As RasterCodecs = New RasterCodecs() 
            viewerControl.Image = codecs.Load(fileStream) 
         Finally 
            fileStream.Dispose() 
         End Try 
      End If 
      Catch ex As Exception 
         MessageBox.Show(ex.Message) 
      End Try 
    End Sub 
                     
         

    C#
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
      try 
      { 
         OpenFileDialog ofd = new OpenFileDialog(); 
         if (ofd.ShowDialog() == true) 
         { 
            using (FileStream fileStream = ofd.File.OpenRead()) 
            { 
               RasterCodecs codecs = new RasterCodecs(); 
               viewerControl.Image = codecs.Load(fileStream); 
            } 
         } 
      } 
      catch (Exception ex) 
      { 
         MessageBox.Show(ex.Message); 
      } 
    } 
                     
         

  10. Build, and Run the program to test it.

    Click the "Load" button, and select an image. NOTE: if you encounter and "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.

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