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.
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:
Imports Load_And_Display.LEADTOOLSWCF
Imports System.ServiceModel
using Load_And_Display.LEADTOOLSWCF;
using System.ServiceModel;
Update the Button_Click event as shown below:
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
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);
}
}
Add the following class function:
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
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.
Products |
Support |
Feedback: Loading And displaying an image using WCF in Silverlight |
Introduction |
Help Version 19.0.2017.6.21
|
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.