←Select platform

RasterImageTypeConverter Class

Summary

RasterImageTypeConverter is a class that can be used to convert RasterImage objects from other data types.

Syntax

C#
VB
C++
public class RasterImageTypeConverter : TypeConverter 
Public Class RasterImageTypeConverter  
   Inherits System.ComponentModel.TypeConverter 
public ref class RasterImageTypeConverter : public System.ComponentModel.TypeConverter  

Remarks

This class derives from the standard .NET TypeConverter class to provides functionality for converting RasterImage objects from other data types.

The following data types are supported:

  • Uri containing the URL to the image file with optional parameters.
  • String containing the path to the image file with optional parameters.
  • Stream containing the data of the image file.
  • An array of Byte containing the data of the image file.

The CanConvertFrom method will return true if any of the supported object types is passed into the sourceType parameter. However, this does not mean that the conversion operation will succeed, this depends on whether the data contains a valid image that LEADTOOLS can load.

The ConvertFrom method will try to convert the object passed into the value parameter to a valid RasterImage object.

For Uri and String source objects, LEADTOOLS supports optional extra parameters passed as standard HTML query string to fine tune the loading of RasterImage objects. The following table lists these parameters:

Parameter Description
LeadBitsPerPixel

Resulting image pixel depth. This should be a string representation of an integer. Valid values are:

value Meaning
0 Keep the original file's pixel depth (Do not convert).
1 to 8 The specified bits per pixel in the resulting image.
12 12 bits per pixel in the resulting image.
16 16 bits per pixel in the resulting image.
24 24 bits per pixel in the resulting image.
32 32 bits per pixel in the resulting image.
48 48 bits per pixel in the resulting image.
64 64 bits per pixel in the resulting image.
LeadOrder

Color order for 16-bit, 24-bit, 32-bit, 48-bit, and 64-bit images. If the resulting image is less than 16 bits per pixel, this will have no effect since palletized images have no order. This must be one of the following string values:

Value Meaning
Rgb Red, green, and blue color order in memory
Bgr Blue, green, and red color order in memory
Gray 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are only supported in the Document/Medical Imaging editions.
RgbOrGray Load the image as red, green, blue OR as a 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions
BgrOrGray Load the image as blue, green, red OR as a 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions
Romm ROMM order. ROMM only supports 24 and 48-bit images
BgrOrGrayOrRomm Load the image as blue, green, red OR as a 12 or 16-bit grayscale image OR as ROMM. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions only. ROMM only supports 24 and 48-bit color images.
LeadFirstPage

A string that contains an integer representation of the 1-based index of the first page to load.

LeadLastPage

A string that contains an integer representation of the 1-based index of last page to load. Must be greater than or equal to "LeadFirstPage" if specified.

LeadUseWriteableBitmap

For Silverlight only: a string that contains a boolean representation of a value that will be used to set CodecsLoadOptions.UseWriteableBitmap. If this is omitted, false will be used. If you add this parameter with a true value, then the resulting RasterImage will be loaded with a "WriteableBitmap" as the backend data. Refer to CodecsLoadOptions.UseWriteableBitmap for more information.

LeadUseNativeLoad

For Silverlight only: a string that contains a boolean representation of a value that will be used to set CodecsLoadOptions.UseNativeLoad. If this is omitted, false will be used. If you add this parameter with a true value, then the resulting RasterImage will be loaded with the native filter codecs instead of LEADTOOLS. Refer to CodecsLoadOptions.UseNativeLoad for more information.

value Meaning
0 Keep the original file's pixel depth (Do not convert).
1 to 8 The specified bits per pixel in the resulting image.
12 12 bits per pixel in the resulting image.
16 16 bits per pixel in the resulting image.
24 24 bits per pixel in the resulting image.
32 32 bits per pixel in the resulting image.
48 48 bits per pixel in the resulting image.
64 64 bits per pixel in the resulting image.
Value Meaning
Rgb Red, green, and blue color order in memory
Bgr Blue, green, and red color order in memory
Gray 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are only supported in the Document/Medical Imaging editions.
RgbOrGray Load the image as red, green, blue OR as a 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions
BgrOrGray Load the image as blue, green, red OR as a 12 or 16-bit grayscale image. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions
Romm ROMM order. ROMM only supports 24 and 48-bit images
BgrOrGrayOrRomm Load the image as blue, green, red OR as a 12 or 16-bit grayscale image OR as ROMM. 12 and 16-bit grayscale images are supported in the Document/Medical Imaging editions only. ROMM only supports 24 and 48-bit color images.

All these parameters are optional and should be passed as a standard HTML query string. The order is not important. Here are a few examples of valid strings:

// Load all the pages in the TIF file hosted at an HTTP URL 
http://tempuri.org/MyImage.tif 
// Load the first page only of the multi-page TIF file hosted at an HTTP URL 
http://tempuri.org/MyImage.tif?LeadFirstPage=1&LeadLastPage=1 
// Load the second and third page of the multi-page TIF file hosted at an HTTP URL as 24 bits/pixel with RGB color order 
http://tempuri.org/MyImage.tif?LeadBitsPerPixel=24&LeadOrder=Rgb&LeadFirstPage=2&LeadLastPage=3 
// Load all the pages in the TIF file hosted at an FTP site 
ftp://tempuri.org/MyImage.tif 
// Load all the pages in the TIF file in a disk file 
C:\Dir\MyImage.tif 
file:://C:/Dir/MyImage.tif 
file:://myserver/MyImage.tif 
// Load the second and third page of the multi-page TIF file in a disk file as 24 bits/pixel with RGB color order 
C:\Dir\MyImage\MyImage.tif?LeadBitsPerPixel=24&LeadOrder=Rgb&LeadFirstPage=2&LeadLastPage=3 

The RasterImageTypeConverter is also used when loading a resource image from XAML as a RasterImage. Here are a few example:

// Load all the pages in the TIF file set as resource 
"MyImage.tif" 
// Load the first page only of the multi-page TIF file set as a resource 
"MyImage.tif?LeadFirstPage=1&LeadLastPage=1" 
// Load the second and third page of the multi-page TIF file set as a resource as 24 bits/pixel with RGB color order 
"MyImage.tif?LeadBitsPerPixel=24&LeadOrder=Rgb&LeadFirstPage=2&LeadLastPage=3" 
// Load the second and third page of the multi-page TIF file set as a resource as 24 bits/pixel with RGB color order 
"MyImage.tif?LeadBitsPerPixel=24&LeadOrder=Rgb&LeadFirstPage=2&LeadLastPage=3" 
// Silverlight only: Load the image and set it as a WriteableBitmap in a RasterImage 
"MyImage.tif?LeadUseWriteableBitmap=True" 
// Silverlight only: Load the PNG image using Silverlight and set it as a WriteableBitmap in a RasterImage 
"MyImage.tif?LeadUseWriteableBitmap=True&LeadUseNativeLoad=True" 

Query string are not supported for String and an array of Byte data types.

The LEADTOOLS WPF and Silverlight controls use the RasterImageTypeConverter to load a RasterImage from a XAML file.

The image returned from the ConvertFrom method will have itsRasterImage.IsLoading property set to true since this method will load the image asynchronously. To get notified when the image has finished loading, create the RasterImageTypeConverter with your own RasterCodecs instance and subscribe to the RasterCodecs.LoadAsyncCompleted event before calling ConvertFrom as show in the example below.

Example

This example will use the RasterImageTypeConverter to load an image from a URL.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
 
public void RasterImageTypeConverterExample() 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"); 
 
   // Construct the source URL to use the source file and load the image 
   Uri uri = new Uri(srcFileName); 
 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Create a new instance of the RasterImageTypeConverter class 
   RasterImageTypeConverter rasterImageConverter = new RasterImageTypeConverter(codecs); 
 
   // We should be able to convert from a URL 
   Debug.Assert(rasterImageConverter.CanConvertFrom(uri.GetType())); 
 
   // Convert the image 
 
   // The return value from RasterImageTypeConverter.ConvertFrom might be an image that is 
   // still loading. So, we must subscribe to the RasterCodecs.LoadAsyncCompleteted 
   // event to obtain the finished image 
   codecs.LoadAsyncCompleted += new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted); 
   object rasterImageObject = rasterImageConverter.ConvertFrom(uri); 
 
   // Notice that the returned rasterImageObject is a RasterImage with IsLoading set to true at this point 
   // The IsLoading will be false (and hence, the object will be usable) when the LoadAsyncCompleteted 
   // fires. 
} 
 
private void rasterImageTypeConverterExample_LoadAsyncCompleted(object sender, CodecsLoadAsyncCompletedEventArgs e) 
{ 
   // Check if the user canceled or if we have errors 
   if (e.Cancelled) 
   { 
      Console.WriteLine("Canceled by the user"); 
   } 
   else if (e.Error != null) 
   { 
      Console.WriteLine("Error: " + e.Error.Message); 
   } 
   else 
   { 
      // Everything is OK, get the image 
      RasterImage image = e.Image; 
 
      Console.WriteLine("Image at {0} loaded OK, size: {1} by {2}", e.Uri, image.Width, image.Height); 
 
      image.Dispose(); 
   } 
 
   // Unsubscribe to the event and dispose the RasterCodecs object 
   RasterCodecs codecs = sender as RasterCodecs; 
   codecs.LoadAsyncCompleted -= new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
 
Public Sub RasterImageTypeConverterExample() 
   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif") 
 
   ' Construct the source URL to use the source file and load the image 
   Dim uri As New Uri(srcFileName) 
 
   Dim codecs As New RasterCodecs() 
 
   ' Create a new instance of the RasterImageTypeConverter class 
   Dim rasterImageConverter As New RasterImageTypeConverter(codecs) 
 
   ' We should be able to convert from a URL 
   Debug.Assert(rasterImageConverter.CanConvertFrom(uri.GetType())) 
 
   ' Convert the image 
 
   ' The return value from RasterImageTypeConverter.ConvertFrom might be an image that is 
   ' still loading. So, we must subscribe to the RasterCodecs.LoadAsyncCompleteted 
   ' event to obtain the finished image 
   AddHandler codecs.LoadAsyncCompleted, AddressOf rasterImageTypeConverterExample_LoadAsyncCompleted 
   Dim rasterImageObject As Object = rasterImageConverter.ConvertFrom(uri) 
 
   ' Notice that the returned rasterImageObject is a RasterImage with IsLoading set to true at this point 
   ' The IsLoading will be false (and hence, the object will be usable) when the LoadAsyncCompleteted 
   ' fires. 
End Sub 
 
Private Sub rasterImageTypeConverterExample_LoadAsyncCompleted(ByVal sender As Object, ByVal e As CodecsLoadAsyncCompletedEventArgs) 
   ' Check if the user canceled or if we have errors 
   If e.Cancelled Then 
      Console.WriteLine("Canceled by the user") 
   ElseIf Not IsNothing(e.Error) Then 
      Console.WriteLine("Error: " + e.Error.Message) 
   Else 
      ' Everything is OK, get the image 
      Dim image As RasterImage = e.Image 
 
      Console.WriteLine("Image at {0} loaded OK, size: {1} by {2}", e.Uri, image.Width, image.Height) 
 
      image.Dispose() 
   End If 
 
   ' Unsubscribe to the event and dispose the RasterCodecs object 
   Dim codecs As RasterCodecs = DirectCast(sender, RasterCodecs) 
   RemoveHandler codecs.LoadAsyncCompleted, AddressOf rasterImageTypeConverterExample_LoadAsyncCompleted 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly