LEADTOOLS WPF and Silverlight Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.22
Working with image streams and files in Silverlight

For security and other reasons, Silverlight offers no direct access to the operating system's file system. Even so, there are still several ways to interact with files, whether they are on disk, in memory, over the web, in databases--virtually any location where images are stored. The following snippets show various ways to load/save images with LEADTOOLS Silverlight.

  1. Using the Microsoft Load and Save Dialogs:

    [Visual Basic]

                'Loading
                Try
                  Dim codecs As RasterCodecs = New RasterCodecs()
                  Dim ofd As OpenFileDialog = New OpenFileDialog()
                  If ofd.ShowDialog() = True Then
                     Dim fileStream As FileStream = ofd.File.OpenRead()
                     Try
                        viewerControl.Image = codecs.Load(fileStream)
                     Finally
                        fileStream.Dispose()
                     End Try
                  End If
                Catch ex As RasterException
                  ' See if LEADTOOLS could not recognize this image format
                  If ex.Code = RasterExceptionCode.FileFormat Then
                     MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS")
                  Else
                     ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                     MessageBox.Show(String.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                  End If
                Catch ex As Exception
                  ' Other errors
                  MessageBox.Show(String.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message))
                End Try
                'Saving 
                Try
                  Dim codecs As RasterCodecs = New RasterCodecs()
                  Dim sfd As SaveFileDialog = New SaveFileDialog()
                  If sfd.ShowDialog() = True Then
                     Dim stream As Stream = sfd.OpenFile()
                     Try
                        codecs.Save(viewerControl.Image, stream, Leadtools.RasterImageFormat.Jpeg, 0)
                     Finally
                        stream.Dispose()
                     End Try
                  End If
                Catch ex As RasterException
                  ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                  MessageBox.Show(String.Format("Error saving file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                Catch ex As Exception
                  ' Other errors
                  MessageBox.Show(String.Format("Error saving file.{0}{1}", Environment.NewLine, ex.Message))
                End Try
                              
                
    

    [C#]

                //Loading
                try
                {
                  RasterCodecs codecs = new RasterCodecs();
                  OpenFileDialog ofd = new OpenFileDialog();
                  if (ofd.ShowDialog() == true)
                  {
                     using (FileStream fileStream = ofd.File.OpenRead())
                        viewerControl.Image = codecs.Load(fileStream);
                  }
                }
                catch (RasterException ex)
                {
                  // See if LEADTOOLS could not recognize this image format
                  if (ex.Code == RasterExceptionCode.FileFormat)
                     MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS");
                  else
                  {
                     // Other LEADTOOLS error (file might be corrupted, read error, etc)
                     MessageBox.Show(string.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                  }
                }
                catch (Exception ex)
                {
                  // Other errors
                  MessageBox.Show(string.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message));
                }
                //Saving
                try
                {
                  RasterCodecs codecs = new RasterCodecs();
                  using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                  using (IsolatedStorageFileStream stream = store.CreateFile("myfile.jpg"))
                     codecs.Save(viewerControl.Image, stream, Leadtools.RasterImageFormat.Jpeg, 0);
                }
                catch (RasterException ex)
                {
                  // Other LEADTOOLS error (file might be corrupted, read error, etc)
                  MessageBox.Show(string.Format("Error saving file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                }
                catch (Exception ex)
                {
                  // Other errors
                  MessageBox.Show(string.Format("Error saving file.{0}{1}", Environment.NewLine, ex.Message));
                }
                              
                
    

  2. Using the Silverlight Isolated Storage System:

    [Visual Basic]

                'Loading
                Try
                  Dim codecs As RasterCodecs = New RasterCodecs()
                  Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
                  Dim stream As IsolatedStorageFileStream = store.OpenFile("myfile.jpg", FileMode.Open, FileAccess.Read)
                  Try
                     viewerControl.Image = codecs.Load(stream)
                  Finally
                     store.Dispose()
                     stream.Dispose()
                  End Try
                Catch ex As RasterException
                  ' See if LEADTOOLS could not recognize this image format
                  If ex.Code = RasterExceptionCode.FileFormat Then
                     MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS")
                  Else
                     ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                     MessageBox.Show(String.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                  End If
                Catch ex As Exception
                  ' Other errors
                  MessageBox.Show(String.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message))
                End Try         
                'Saving
                Try
                  Dim codecs As RasterCodecs = New RasterCodecs()
                  Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
                  Dim stream As IsolatedStorageFileStream = store.CreateFile("myfile.jpg")
                  Try
                     codecs.Save(viewerControl.Image, stream, Leadtools.RasterImageFormat.Jpeg, 0)
                  Finally
                     store.Dispose()
                     stream.Dispose()
                  End Try
                Catch ex As RasterException
                  ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                  MessageBox.Show(String.Format("Error saving file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                Catch ex As Exception
                  ' Other errors
                  MessageBox.Show(String.Format("Error saving file.{0}{1}", Environment.NewLine, ex.Message))
                End Try
                              
                
    

    [C#]

                //Loading
                try
                {
                  RasterCodecs codecs = new RasterCodecs();
                  using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                  using (IsolatedStorageFileStream stream = store.OpenFile("myfile.jpg", FileMode.Open, FileAccess.Read))
                     viewerControl.Image = codecs.Load(stream);
                }
                catch (RasterException ex)
                {
                  // See if LEADTOOLS could not recognize this image format
                  if (ex.Code == RasterExceptionCode.FileFormat)
                     MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS");
                  else
                  {
                     // Other LEADTOOLS error (file might be corrupted, read error, etc)
                     MessageBox.Show(string.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                  }
                }
                catch (Exception ex)
                {
                  // Other errors
                  MessageBox.Show(string.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message));
                }
                //Saving 
                try
                {
                  RasterCodecs codecs = new RasterCodecs();
                  SaveFileDialog sfd = new SaveFileDialog();
                  if (sfd.ShowDialog() == true)
                  {
                     using (Stream stream = sfd.OpenFile())
                        codecs.Save(viewerControl.Image, stream, Leadtools.RasterImageFormat.Jpeg, 0);
                  }
                }
                catch (RasterException ex)
                {
                  // Other LEADTOOLS error (file might be corrupted, read error, etc)
                  MessageBox.Show(string.Format("Error saving file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                }
                catch (Exception ex)
                {
                  // Other errors
                  MessageBox.Show(string.Format("Error saving file.{0}{1}", Environment.NewLine, ex.Message));
                }
                              
                
    

  3. Using the WebClient Class to download images:

    [Visual Basic]

                ' You may need a cross domain policy file on the server containing the image in order to use the WebClient Class.
                Dim fileDownloader As WebClient = New WebClient()
                AddHandler fileDownloader.OpenReadCompleted, AddressOf fileDownloader_OpenReadCompleted
                fileDownloader.OpenReadAsync(New Uri("http://demo.leadtools.com/images/cmp/image1.cmp"), Nothing)
                Private Sub fileDownloader_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
                  Try
                     Dim codecs As RasterCodecs = New RasterCodecs()
                        viewerControl.Image = codecs.Load(e.Result)
                  Catch ex As RasterException
                     ' See if LEADTOOLS could not recognize this image format
                     If ex.Code = RasterExceptionCode.FileFormat Then
                        MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS")
                     Else
                        ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                        MessageBox.Show(String.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                     End If
                  Catch ex As Exception
                     ' Other errors
                     MessageBox.Show(String.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message))
                  End Try
                End Sub                
                              
                
    

    [C#]

                // You may need a cross domain policy file on the server containing the image in order to use the WebClient Class.
                WebClient fileDownloader = new WebClient();
                fileDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(fileDownloader_OpenReadCompleted);
                fileDownloader.OpenReadAsync(new Uri("http://demo.leadtools.com/images/cmp/image1.cmp"), null);
                void fileDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
                {
                  try
                  {
                     RasterCodecs codecs = new RasterCodecs();
                     viewerControl.Image = codecs.Load(e.Result);
                  }
                  catch (RasterException ex)
                  {
                     // See if LEADTOOLS could not recognize this image format
                     if (ex.Code == RasterExceptionCode.FileFormat)
                        MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS");
                     else
                     {
                        // Other LEADTOOLS error (file might be corrupted, read error, etc)
                        MessageBox.Show(string.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                     }
                  }
                  catch (Exception ex)
                  {
                     // Other errors
                     MessageBox.Show(string.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message));
                  }
                }
                              
                
    

  4. Using a WCF or standard web service:

    [Visual Basic]

                'This is the completed event when calling the LEADTOOLS RasterService WCF Service. 
                'LEADTOOLS WCF Services can be used for loading files that the Silverlight library 
                'does not support, for writing barcodes, and much more. Please see Loading And Displaying An Image Using WCF In Silverlight for the complete tutorial.
                Private Sub client_ConvertCompleted(ByVal sender As Object, ByVal e As ConvertCompletedEventArgs)
                  Try
                     If TypeOf e.Result Is ConvertResponse Then
                        Dim convertResponse As ConvertResponse = CType(IIf(TypeOf e.Result Is ConvertResponse, e.Result, Nothing), ConvertResponse)
                        'Image has been sent back from the service.
                        'Load the image into the viewer.
                        If TypeOf convertResponse.Destination Is RawBinaryData Then
                           Dim ms As MemoryStream = New MemoryStream((CType(IIf(TypeOf convertResponse.Destination Is RawBinaryData, convertResponse.Destination, Nothing), RawBinaryData)).Data)
                           Try
                               viewerControl.Image = codecs.Load(ms)
                           Finally
                              ms.Dispose()
                           End Try
                        End If
                     End If
                  Catch ex As RasterException
                     ' See if LEADTOOLS could not recognize this image format
                     If ex.Code = RasterExceptionCode.FileFormat Then
                        MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS")
                     Else
                        ' Other LEADTOOLS error (file might be corrupted, read error, etc)
                        MessageBox.Show(String.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message))
                     End If
                  Catch ex As Exception
                     ' Other errors
                     MessageBox.Show(String.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message))
                  End Try
                End Sub           
                              
                
    

    [C#]

                //This is the completed event when calling the LEADTOOLS RasterService WCF Service. 
                //LEADTOOLS WCF Services can be used for loading files that the Silverlight library 
                //does not support, for writing barcodes, and much more. Please see Loading And Displaying An Image Using WCF In Silverlight for the complete tutorial.
                void client_ConvertCompleted(object sender, ConvertCompletedEventArgs e)
                {
                  try
                  {
                     if (e.Result is ConvertResponse)
                     {
                        ConvertResponse convertResponse = e.Result as ConvertResponse;
                        //Image has been sent back from the service.
                        //Load the image into the viewer.
                        if (convertResponse.Destination is RawBinaryData)
                        {
                           using (MemoryStream ms = new MemoryStream((convertResponse.Destination as RawBinaryData).Data))
                              viewerControl.Image = codecs.Load(ms);
                        }
                     }
                  }
                  catch (RasterException ex)
                  {
                     // See if LEADTOOLS could not recognize this image format
                     if (ex.Code == RasterExceptionCode.FileFormat)
                        MessageBox.Show("File does not contain an image format recognizable by LEADTOOLS");
                     else
                     {
                        // Other LEADTOOLS error (file might be corrupted, read error, etc)
                        MessageBox.Show(string.Format("Error loading file.{0}Leadtools code: {1}{0}Message: {2}", Environment.NewLine, ex.Code, ex.Message));
                     }
                  }
                  catch (Exception ex)
                  {
                     // Other errors
                     MessageBox.Show(string.Format("Error loading file.{0}{1}", Environment.NewLine, ex.Message));
                  }
                }