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.
Using the Microsoft Load and Save Dialogs:
'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
//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));
}
Using the Silverlight Isolated Storage System:
'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
//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));
}
Using the WebClient Class to download images:
' 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
// 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));
}
}
Using a WCF or standard web service:
'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
//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));
}
}
Products |
Support |
Feedback: Working with image streams and files 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.