LEADTOOLS Image File Support (Leadtools.Codecs assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
RasterImageTypeConverter Class
See Also  Members  
Leadtools.Codecs Namespace : RasterImageTypeConverter Class



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

Object Model

RasterImageTypeConverter Class

Syntax

Visual Basic (Declaration) 
Public Class RasterImageTypeConverter 
   Inherits System.ComponentModel.TypeConverter
Visual Basic (Usage)Copy Code
Dim instance As RasterImageTypeConverter
C# 
public class RasterImageTypeConverter : System.ComponentModel.TypeConverter 
C++/CLI 
public ref class RasterImageTypeConverter : public System.ComponentModel.TypeConverter 

Example

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

Visual BasicCopy Code
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
C#Copy Code
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";
}
SilverlightCSharpCopy Code
SilverlightVBCopy Code

Remarks

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

The following data types are supported:

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

The CanConvertFrom(ITypeDescriptorContext,Type) 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(ITypeDescriptorContext,CultureInfo,Object) method will try to convert the object passed into the value parameter to a valid Leadtools.RasterImage object.

For System.Uri and System.String source objects, LEADTOOLS supports optional extra parameters passed as standard HTML query string to fine tune the loading of Leadtools.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-, 24-, 32-, 48-, 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 red, green, blue 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.

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
            

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

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

The image returned from the ConvertFrom(ITypeDescriptorContext,CultureInfo,Object) method will have its RasterImage.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(ITypeDescriptorContext,CultureInfo,Object) as show in the example below.

Inheritance Hierarchy

System.Object
   System.ComponentModel.TypeConverter
      Leadtools.Codecs.RasterImageTypeConverter

Requirements

Target Platforms: Silverlight, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only)

See Also