LEADTOOLS For .NET Class Library Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.28
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 the various interactive modes of the LEADTOOLS Silverlight Control and the Pan Viewer:

  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, please see WCF Tutorials.

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:

[Visual Basic]

            Imports Load_And_Display.LEADTOOLSWCF
            Imports System.ServiceModel
            

[C#]

            using Load_And_Display.LEADTOOLSWCF;
            using System.ServiceModel;
            

Update the Button_Click event as shown below:

[Visual Basic]

            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()
                    Dim buffer As Byte() = New Byte(fileStream.Length - 1){}
                    fileStream.Read(buffer, 0, CInt(fileStream.Length))
                    fileStream.Close()
                    '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 = 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)
                 {
                    FileStream fileStream = ofd.File.OpenRead();
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, (int)fileStream.Length);
                    fileStream.Close();
                    //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 = 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);
              }
            }
            

Add the following class function:

[Visual Basic]

            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 in a Silverlight-friendly format.
                    '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)
                       Dim bi As BitmapImage = New BitmapImage()
                       bi.SetSource(ms)
                       viewerControl.Source = bi
                       ms.Close()
                    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 in a Silverlight-friendly format.
                    //Load the image into the viewer.
                    if (convertResponse.Destination is RawBinaryData)
                    {
                       MemoryStream ms = new MemoryStream((convertResponse.Destination as RawBinaryData).Data);
                       BitmapImage bi = new BitmapImage();
                       bi.SetSource(ms);
                       viewerControl.Source = bi;
                       ms.Close();
                    }
                 }
              }
              catch (Exception ex)
              {
                 MessageBox.Show(ex.Message);
              }
            }
            

Build, and Run the program to test it.

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

Notes:

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.

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 http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx.