Loading And displaying an image using WCF in Silverlight

Take the following steps to start a project and to add some code that will demonstrate how to consume LEADTOOLS WCF Services from a Silverlight application to load and display images. LEADTOOLS includes native Silverlight codecs for most formats so most image formats can be decoded and viewed natively within the Silverlight application without the WCF Service. In cases where your application needs to load a format for which LEADTOOLS does not provide a native Silverlight codec however, you can still use LEADTOOLS WCF Services to decode the image on the server and send the image back in a format which LEADTOOLS provides a native codec for.

  1. Start Visual Studio .NET.
  2. Start with the project that you created in Loading and displaying an image in Silverlight
  3. In the "Solution Explorer" window, right-click on the Silverlight project and select Add Service Reference from the context menu. Enter the location where the LEADTOOLS RasterService is being hosted. By default LEADTOOLS installation will host all services in 'http://localhost/LEADTOOLSWCFServices/RasterService.svc'. Enter "LEADTOOLSWCF" for the namespace and click OK. For more information on hosting and consuming LEADTOOLS WCF services, refer to WCF Tutorials.
  4. Switch to MainPage.xaml code view (right-click MainPage.xaml in the solution explorer then select View Code) and add the following lines at the beginning of the file:

VB
Imports Load_And_Display.LEADTOOLSWCF 
Imports System.ServiceModel 

C#
using Load_And_Display.LEADTOOLSWCF; 
using System.ServiceModel; 

  1. Update the Button_Click event as shown below:

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 buffer As Byte() = Nothing 
        Dim fileStream As FileStream = ofd.File.OpenRead() 
        Try 
           buffer = New Byte(fileStream.Length - 1){} 
           fileStream.Read(buffer, 0, CInt(fileStream.Length)) 
        Finally 
           fileStream.Dispose() 
        End Try 
        'Create RasterService and set options 
        Dim client As RasterServiceClient = New RasterServiceClient() 
        Dim binding As BasicHttpBinding = New BasicHttpBinding() 
        binding.MaxReceivedMessageSize = 50000000 
        client.Endpoint.Binding = binding 
        AddHandler client.ConvertCompleted, AddressOf Of ConvertCompletedEventArgs 
        Dim source As RawBinaryData = New RawBinaryData() 
        source.Data = buffer 
        'Create RasterConvertOptions and set conversion options 
        Dim convertOptions As RasterConvertOptions = New RasterConvertOptions() 
        convertOptions.Source = source 
        convertOptions.Destination = Nothing 
        convertOptions.BitsPerPixel = 24 
        convertOptions.Format = Load_And_Display.LEADTOOLSWCF.RasterImageFormat.Png 
        convertOptions.FirstPage = 1 
        convertOptions.LastPage = 1 
        Dim request As ConvertRequest = New ConvertRequest() 
        request.ConvertOptions = convertOptions 
        'Call service 
        client.ConvertAsync(request) 
     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) 
     { 
        byte[] buffer = null; 
        using (FileStream fileStream = ofd.File.OpenRead()) 
        { 
           buffer = new byte[fileStream.Length]; 
           fileStream.Read(buffer, 0, (int)fileStream.Length); 
        } 
        //Create RasterService and set options 
        RasterServiceClient client = new RasterServiceClient(); 
        BasicHttpBinding binding = new BasicHttpBinding(); 
        binding.MaxReceivedMessageSize = 50000000; 
        client.Endpoint.Binding = binding; 
        client.ConvertCompleted += new EventHandler<ConvertCompletedEventArgs>(client_ConvertCompleted); 
        RawBinaryData source = new RawBinaryData(); 
        source.Data = buffer; 
        //Create RasterConvertOptions and set conversion options 
        RasterConvertOptions convertOptions = new RasterConvertOptions(); 
        convertOptions.Source = source; 
        convertOptions.Destination = null; 
        convertOptions.BitsPerPixel = 24; 
        convertOptions.Format = Load_And_Display.LEADTOOLSWCF.RasterImageFormat.Png; 
        convertOptions.FirstPage = 1; 
        convertOptions.LastPage = 1; 
        ConvertRequest request = new ConvertRequest(); 
        request.ConvertOptions = convertOptions; 
        //Call service 
        client.ConvertAsync(request); 
     } 
  } 
  catch (Exception ex) 
  { 
     MessageBox.Show(ex.Message); 
  } 
} 

  1. Add the following class function:

VB
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 
              Dim codecs As RasterCodecs = New RasterCodecs() 
              viewerControl.Image = codecs.Load(ms) 
           Finally 
              ms.Dispose() 
           End Try 
        End If 
     End If 
  Catch ex As Exception 
     MessageBox.Show(ex.Message) 
  End Try 
End Sub 

C#
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)) 
           { 
              RasterCodecs codecs = new RasterCodecs(); 
              viewerControl.Image = codecs.Load(ms); 
           } 
        } 
     } 
  } 
  catch (Exception ex) 
  { 
     MessageBox.Show(ex.Message); 
  } 
} 

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

  2. Click the "Load" button, and select any image that LEADTOOLS supports.

Notes:

  1. By default, the application will look for the service in the same address from which it was originally added. You can change this address using the RasterServiceClient.Endpoint.Address Property.

  2. If you receive a cross domain exception, you may need to add clientaccesspolicy.xml to the root of your web domain. For more information, please visit https://msdn.microsoft.com/en-us/library/cc197955.aspx.

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