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.
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.
[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 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); } }
[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. '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); } }
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.