LEADTOOLS Image File Support (Leadtools.Codecs assembly)
LEAD Technologies, Inc

LoadInformation Event

Example 





Occurs during loading of a file for which LEADTOOLS cannot recognize the format. .NET support WinRT support Silverlight support
Syntax
public event EventHandler<CodecsLoadInformationEventArgs> LoadInformation
'Declaration
 
Public Event LoadInformation As EventHandler(Of CodecsLoadInformationEventArgs)
'Usage
 
Dim instance As RasterCodecs
Dim handler As EventHandler(Of CodecsLoadInformationEventArgs)
 
AddHandler instance.LoadInformation, handler
public event EventHandler<CodecsLoadInformationEventArgs> LoadInformation
add_LoadInformation(function(sender, e))
remove_LoadInformation(function(sender, e))

public:
event EventHandler<CodecsLoadInformationEventArgs^>^ LoadInformation
Event Data

The event handler receives an argument of type CodecsLoadInformationEventArgs containing data related to this event. The following CodecsLoadInformationEventArgs properties provide information specific to this event.

PropertyDescription
BitsPerPixel Gets or sets the bits per pixel of the image data in the file. .NET support WinRT support Silverlight support
Format Gets the image file format. .NET support WinRT support Silverlight support
Height Gets or sets the image height in pixels. .NET support WinRT support Silverlight support
LeastSignificantBitFirst Gets or sets a value specifying whether the image data was saved with least significant bit first or last. .NET support WinRT support Silverlight support
MotorolaOrder Gets or sets a value that determines whether or not image data is in Motorola byte order. .NET support WinRT support Silverlight support
Offset Gets or sets the position of the first byte of image data. .NET support WinRT support Silverlight support
Order Gets or sets the byte order for the load process. .NET support WinRT support Silverlight support
Pad4 Gets or sets a value indicating whether the image data was saved padded to 4-byte boundary. .NET support WinRT support Silverlight support
PhotoInterpolation Gets or sets the photometric interpolation for the load process. .NET support WinRT support Silverlight support
PlanarConfiguration Gets or sets the planar configuration. .NET support WinRT support Silverlight support
Reverse Gets or sets a value that determines whether each line is reversed (mirrored). .NET support WinRT support Silverlight support
Signed Gets or sets a value that determines whether or not the image data is signed. .NET support WinRT support
Stream Gets the stream used by the load process. .NET support Silverlight support
Stream Gets the stream used by the load process. WinRT support
StripSize Gets or sets the size of the data strip before it is decompressed. .NET support WinRT support Silverlight support
ViewPerspective Gets or sets the view perspective for the load process. .NET support WinRT support Silverlight support
WhiteOnBlack Gets or sets a value that determines if the image is black-on-white or white on black. .NET support WinRT support Silverlight support
Width Gets or sets the image width in pixels. .NET support WinRT support Silverlight support
XResolution Gets or sets the horizontal resolution for rendering the image, specified in dots per inch. .NET support WinRT support Silverlight support
YResolution Gets or sets the vertical resolution for rendering the image, specified in dots per inch. .NET support WinRT support Silverlight support
Remarks

The LoadInformation event will occur when LEADTOOLS cannot recognize the image file format during a call to GetInformation(String,Boolean) or GetInformationAsync(String,Boolean,Int32,Object). This event can be used for loading raw FAX data (CCITT Group 3 or Group 4), raw run-length-encoded data (4-bit or 8-bit), raw Bitfield compressed data, raw PackBits compressed data or raw uncompressed data.

To load a raw fax file, you must subscribe to this event first. When the toolkit fails to recognize the image file format, it will fire this event. If you know the size of the fax image, then set the values in the CodecsLoadInformationEventArgs.Width and CodecsLoadInformationEventArgs.Height properties of the CodecsLoadInformationEventArgs passed as the event data. To automatically detect the width and height of the fax file, set CodecsLoadInformationEventArgs.Width and CodecsLoadInformationEventArgs.Height to -1.

To load raw uncompressed data, you must subscribe to this event first. When the toolkit fails to recognize the image file format, it will fire this event. Set the CodecsLoadInformationEventArgs.Format property to RasterImageFormat.Raw. Valid values must also be set for the following properties:

If each line of RAW data is padded so that the number of bytes is a multiple of 4 (as is the case with raw Windows BMP data), set CodecsLoadInformationEventArgs.Pad4 to true. Include an orientation in the CodecsLoadInformationEventArgs.ViewPerspective to load with the proper orientation. For example, raw Windows BMP data is stored with a RasterViewPerspective.BottomLeft orientation. If the orientation is unknown, then set it to RasterViewPerspective.BottomLeft. If the raw data is 8 bits per pixel or less, then the image is palettized and a palette must be generated. If this is the case, pass in a valid palette with 1 >gt; bits/pixel number of entries to the CodecsLoadInformationEventArgs.SetPalette method.

Set the correct color order in the CodecsLoadInformationEventArgs.Order property. For example, if the order of the data is BGR, then set this value to RasterByteOrder.Bgr.

For more information, refer to CodecsLoadInformationEventArgs.

Example
 
Private Structure RawData
      Public Width As Integer ' Width of image
      Public Height As Integer ' Height of image
      Public BitsPerPixel As Integer ' Bits per pixel of image--if palettized, a gray palette is generated
      Public ViewPerspective As RasterViewPerspective ' View perspective of raw data (TopLeft, BottomLeft, etc)
      Public Order As RasterByteOrder ' Rgb or Bgr
      Public XResolution As Integer ' Horizontal resolution in DPI
      Public YResolution As Integer ' Vertical resolution in DPI
      Public Offset As Integer ' Offset into file where raw data begins
      Public Padding As Boolean ' true if each line of data is padded to four bytes
      Public ReverseBits As Boolean ' true if the bits of each byte are reversed
   End Structure
   Private myRawData As RawData

   Private Sub codecs_LoadInformation(ByVal sender As Object, ByVal e As CodecsLoadInformationEventArgs)
      ' Set the information
      e.Format = RasterImageFormat.Raw
      e.Width = myRawData.Width
      e.Height = myRawData.Height
      e.BitsPerPixel = myRawData.BitsPerPixel
      e.XResolution = myRawData.XResolution
      e.YResolution = myRawData.YResolution
      e.Offset = myRawData.Offset

      If myRawData.Padding Then
         e.Pad4 = True
      End If

      e.Order = myRawData.Order

      If myRawData.ReverseBits Then
         e.LeastSignificantBitFirst = True
      End If

      e.ViewPerspective = myRawData.ViewPerspective

      ' If image is palettized create a grayscale palette
      If e.BitsPerPixel <= 8 Then
         Dim colors As Integer = 1 << e.BitsPerPixel
         Dim palette As RasterColor() = New RasterColor(colors - 1) {}
         Dim i As Integer = 0
         Do While i < palette.Length
            Dim val As Byte = CByte((i * 255) / (colors - 1))
            palette(i) = New RasterColor(val, val, val)
            i += 1
         Loop

         e.SetPalette(palette)
      End If
   End Sub


   Public Sub LoadInformationExample()
      Dim codecs As RasterCodecs = New RasterCodecs()

      ' First, load a JPEG or CMP file
      Dim srcFilename As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim rawFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.raw")
      Dim image As RasterImage = codecs.Load(srcFilename)

      ' Save this image as RAW data

      ' Set RAW options
      codecs.Options.Raw.Save.Pad4 = True
      codecs.Options.Raw.Save.ReverseBits = True
      codecs.Options.Save.OptimizedPalette = True

      codecs.Save(image, rawFileName, RasterImageFormat.Raw, 0)

      ' Save information about this image so we can use it to load the RAW file
      myRawData = New RawData()
      myRawData.Width = image.Width
      myRawData.Height = image.Height
      myRawData.BitsPerPixel = image.BitsPerPixel
      myRawData.ViewPerspective = image.ViewPerspective
      myRawData.Order = image.Order
      myRawData.XResolution = image.XResolution
      myRawData.YResolution = image.YResolution
      myRawData.Offset = 0
      myRawData.Padding = True
      myRawData.ReverseBits = True

      ' We dont need the image anymore
      image.Dispose()

      ' Now load this RAW image back

      ' First subscribe to the LoadInformation event so we can set the image information
      AddHandler codecs.LoadInformation, AddressOf codecs_LoadInformation

      ' Load the image
      image = codecs.Load(rawFileName)

      ' Unsubscribe from the event
      RemoveHandler codecs.LoadInformation, AddressOf codecs_LoadInformation

      ' save it as bmp for testing
      codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_raw.bmp"), RasterImageFormat.Bmp, 0)

      ' Clean up
      image.Dispose()
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
private struct RawData
     {
         public int Width;                               // Width of image
         public int Height;                              // Height of image
         public int BitsPerPixel;                        // Bits per pixel of image--if palettized, a gray palette is generated
         public RasterViewPerspective ViewPerspective;   // View perspective of raw data (TopLeft, BottomLeft, etc)
         public RasterByteOrder Order;                   // Rgb or Bgr
         public int XResolution;                         // Horizontal resolution in DPI
         public int YResolution;                         // Vertical resolution in DPI
         public int Offset;                              // Offset into file where raw data begins
         public bool Padding;                            // true if each line of data is padded to four bytes
         public bool ReverseBits;                        // true if the bits of each byte are reversed 
     }
     private RawData myRawData;

     private void codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e)
     {
         // Set the information
         e.Format = RasterImageFormat.Raw;
         e.Width = myRawData.Width;
         e.Height = myRawData.Height;
         e.BitsPerPixel = myRawData.BitsPerPixel;
         e.XResolution = myRawData.XResolution;
         e.YResolution = myRawData.YResolution;
         e.Offset = myRawData.Offset;

         if (myRawData.Padding)
             e.Pad4 = true;

         e.Order = myRawData.Order;

         if (myRawData.ReverseBits)
             e.LeastSignificantBitFirst = true;

         e.ViewPerspective = myRawData.ViewPerspective;

         // If image is palettized create a grayscale palette
         if (e.BitsPerPixel <= 8)
         {
             int colors = 1 << e.BitsPerPixel;
             RasterColor[] palette = new RasterColor[colors];
             for (int i = 0; i < palette.Length; i++)
             {
                 byte val = (byte)((i * 255) / (colors - 1));
                 palette[i] = new RasterColor(val, val, val);
             }

             e.SetPalette(palette);
         }
     }


     public void LoadInformationExample()
     {
         RasterCodecs codecs = new RasterCodecs();

         // First, load a JPEG or CMP file
         string srcFilename = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
         string rawFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.raw");
         RasterImage image = codecs.Load(srcFilename);

         // Save this image as RAW data

         // Set RAW options
         codecs.Options.Raw.Save.Pad4 = true;
         codecs.Options.Raw.Save.ReverseBits = true;
         codecs.Options.Save.OptimizedPalette = true;

         codecs.Save(image, rawFileName, RasterImageFormat.Raw, 0);

         // Save information about this image so we can use it to load the RAW file
         myRawData = new RawData();
         myRawData.Width = image.Width;
         myRawData.Height = image.Height;
         myRawData.BitsPerPixel = image.BitsPerPixel;
         myRawData.ViewPerspective = image.ViewPerspective;
         myRawData.Order = image.Order;
         myRawData.XResolution = image.XResolution;
         myRawData.YResolution = image.YResolution;
         myRawData.Offset = 0;
         myRawData.Padding = true;
         myRawData.ReverseBits = true;

         // We dont need the image anymore
         image.Dispose();

         // Now load this RAW image back

         // First subscribe to the LoadInformation event so we can set the image information
         codecs.LoadInformation += new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

         // Load the image
         image = codecs.Load(rawFileName);

         // Unsubscribe from the event
         codecs.LoadInformation -= new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

         // save it as bmp for testing
         codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_raw.bmp"), RasterImageFormat.Bmp, 0);

         // Clean up
         image.Dispose();
         codecs.Dispose();
     }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
private struct RawData
{
   public int Width;                               // Width of image
   public int Height;                              // Height of image
   public int BitsPerPixel;                        // Bits per pixel of image--if palettized, a gray palette is generated
   public RasterViewPerspective ViewPerspective;   // View perspective of raw data (TopLeft, BottomLeft, etc)
   public RasterByteOrder Order;                   // Rgb or Bgr
   public int XResolution;                         // Horizontal resolution in DPI
   public int YResolution;                         // Vertical resolution in DPI
   public int Offset;                              // Offset into file where raw data begins
   public bool Padding;                            // true if each line of data is padded to four bytes
   public bool ReverseBits;                        // true if the bits of each byte are reversed 
}
private RawData myRawData;

private void codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e)
{
   // Set the information
   e.Format = RasterImageFormat.Raw;
   e.Width = myRawData.Width;
   e.Height = myRawData.Height;
   e.BitsPerPixel = myRawData.BitsPerPixel;
   e.XResolution = myRawData.XResolution;
   e.YResolution = myRawData.YResolution;
   e.Offset = myRawData.Offset;

   if (myRawData.Padding)
      e.Pad4 = true;

   e.Order = myRawData.Order;

   if (myRawData.ReverseBits)
      e.LeastSignificantBitFirst = true;

   e.ViewPerspective = myRawData.ViewPerspective;

   // If image is palettized create a grayscale palette
   if (e.BitsPerPixel <= 8)
   {
      int colors = 1 << e.BitsPerPixel;
      RasterColor[] palette = new RasterColor[colors];
      for (int i = 0; i < palette.Length; i++)
      {
         byte val = (byte)((i * 255) / (colors - 1));
         palette[i] = RasterColorHelper.Create(val, val, val);
      }

      e.SetPalette(palette);
   }
}

[TestMethod]
public async Task LoadInformationExample()
{
   RasterCodecs codecs = new RasterCodecs();

   // First, load a JPEG or CMP file
   string srcFileName = @"Assets\Image1.cmp";
   string rawFileName = @"Image1.raw";
   string bmpFileName = @"Image1_raw.bmp";

   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));

   // Save this image as RAW data

   // Set RAW options
   codecs.Options.Raw.Save.Pad4 = true;
   codecs.Options.Raw.Save.ReverseBits = true;
   codecs.Options.Save.OptimizedPalette = true;

   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(rawFileName);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Raw, 0);

   // Save information about this image so we can use it to load the RAW file
   myRawData = new RawData();
   myRawData.Width = image.Width;
   myRawData.Height = image.Height;
   myRawData.BitsPerPixel = image.BitsPerPixel;
   myRawData.ViewPerspective = image.ViewPerspective;
   myRawData.Order = image.Order;
   myRawData.XResolution = image.XResolution;
   myRawData.YResolution = image.YResolution;
   myRawData.Offset = 0;
   myRawData.Padding = true;
   myRawData.ReverseBits = true;

   // We dont need the image anymore
   image.Dispose();

   // Now load this RAW image back

   // First subscribe to the LoadInformation event so we can set the image information
   codecs.LoadInformation += new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

   // Load the image
   loadFile = await Tools.AppInstallFolder.GetFileAsync(rawFileName);
   image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));

   // Unsubscribe from the event
   codecs.LoadInformation -= new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

   // save it as bmp for testing
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(bmpFileName);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 0);

   // Clean up
   image.Dispose();
   codecs.Dispose();
}
private struct RawData
{
   public int Width;                               // Width of image
   public int Height;                              // Height of image
   public int BitsPerPixel;                        // Bits per pixel of image--if palettized, a gray palette is generated
   public RasterViewPerspective ViewPerspective;   // View perspective of raw data (TopLeft, BottomLeft, etc)
   public RasterByteOrder Order;                   // Rgb or Bgr
   public int XResolution;                         // Horizontal resolution in DPI
   public int YResolution;                         // Vertical resolution in DPI
   public int Offset;                              // Offset into file where raw data begins
   public bool Padding;                            // true if each line of data is padded to four bytes
   public bool ReverseBits;                        // true if the bits of each byte are reversed 
}
private RawData myRawData;

private void codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e)
{
   // Set the information
   e.Format = RasterImageFormat.Raw;
   e.Width = myRawData.Width;
   e.Height = myRawData.Height;
   e.BitsPerPixel = myRawData.BitsPerPixel;
   e.XResolution = myRawData.XResolution;
   e.YResolution = myRawData.YResolution;
   e.Offset = myRawData.Offset;

   if(myRawData.Padding)
      e.Pad4 = true;

   e.Order = myRawData.Order;

   if(myRawData.ReverseBits)
      e.LeastSignificantBitFirst = true;

   e.ViewPerspective = myRawData.ViewPerspective;

   // If image is palettized create a grayscale palette
   if(e.BitsPerPixel <= 8)
   {
      int colors = 1 << e.BitsPerPixel;
      RasterColor[] palette = new RasterColor[colors];
      for(int i = 0; i < palette.Length; i++)
      {
         byte val = (byte)((i * 255) / (colors - 1));
         palette[i] = new RasterColor(val, val, val);
      }

      e.SetPalette(palette);
   }
}

public void LoadInformationExample(Stream inStreamCmp, Stream outStreamRaw, Stream outStreamBmp)
{
   RasterCodecs codecs = new RasterCodecs();

   // First, load a JPEG or CMP file
   RasterImage image = codecs.Load(inStreamCmp);

   // Save this image as RAW data

   // Set RAW options
   codecs.Options.Raw.Save.Pad4 = true;
   codecs.Options.Raw.Save.ReverseBits = true;
   codecs.Options.Save.OptimizedPalette = true;

   codecs.Save(image, outStreamRaw, RasterImageFormat.Raw, 0);

   // Save information about this image so we can use it to load the RAW file
   myRawData = new RawData();
   myRawData.Width = image.Width;
   myRawData.Height = image.Height;
   myRawData.BitsPerPixel = image.BitsPerPixel;
   myRawData.ViewPerspective = image.ViewPerspective;
   myRawData.Order = image.Order;
   myRawData.XResolution = image.XResolution;
   myRawData.YResolution = image.YResolution;
   myRawData.Offset = 0;
   myRawData.Padding = true;
   myRawData.ReverseBits = true;

   // We dont need the image anymore
   image.Dispose();

   // Now load this RAW image back

   // First subscribe to the LoadInformation event so we can set the image information
   codecs.LoadInformation += new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

   // Load the image
   image = codecs.Load(outStreamRaw);

   // Unsubscribe from the event
   codecs.LoadInformation -= new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);

   // save it as bmp for testing
   codecs.Save(image, outStreamBmp, RasterImageFormat.Bmp, 0);

   // Clean up
   image.Dispose();
}
Private Structure RawData
   Public Width As Integer ' Width of image
   Public Height As Integer ' Height of image
   Public BitsPerPixel As Integer ' Bits per pixel of image--if palettized, a gray palette is generated
   Public ViewPerspective As RasterViewPerspective ' View perspective of raw data (TopLeft, BottomLeft, etc)
   Public Order As RasterByteOrder ' Rgb or Bgr
   Public XResolution As Integer ' Horizontal resolution in DPI
   Public YResolution As Integer ' Vertical resolution in DPI
   Public Offset As Integer ' Offset into file where raw data begins
   Public Padding As Boolean ' true if each line of data is padded to four bytes
   Public ReverseBits As Boolean ' true if the bits of each byte are reversed
End Structure
Private myRawData As RawData

Private Sub codecs_LoadInformation(ByVal sender As Object, ByVal e As CodecsLoadInformationEventArgs)
   ' Set the information
   e.Format = RasterImageFormat.Raw
   e.Width = myRawData.Width
   e.Height = myRawData.Height
   e.BitsPerPixel = myRawData.BitsPerPixel
   e.XResolution = myRawData.XResolution
   e.YResolution = myRawData.YResolution
   e.Offset = myRawData.Offset

   If myRawData.Padding Then
      e.Pad4 = True
   End If

   e.Order = myRawData.Order

   If myRawData.ReverseBits Then
      e.LeastSignificantBitFirst = True
   End If

   e.ViewPerspective = myRawData.ViewPerspective

   ' If image is palettized create a grayscale palette
   If e.BitsPerPixel <= 8 Then
      Dim colors As Integer = 1 << e.BitsPerPixel
      Dim palette As RasterColor() = New RasterColor(colors - 1){}
      Dim i As Integer = 0
      Do While i < palette.Length
         Dim val As Byte = CByte((i * 255) / (colors - 1))
         palette(i) = New RasterColor(val, val, val)
         i += 1
      Loop

      e.SetPalette(palette)
   End If
End Sub

Public Sub LoadInformationExample(ByVal inStreamCmp As Stream, ByVal outStreamRaw As Stream, ByVal outStreamBmp As Stream)
   Dim codecs As RasterCodecs = New RasterCodecs()

   ' First, load a JPEG or CMP file
   Dim image As RasterImage = codecs.Load(inStreamCmp)

   ' Save this image as RAW data

   ' Set RAW options
   codecs.Options.Raw.Save.Pad4 = True
   codecs.Options.Raw.Save.ReverseBits = True
   codecs.Options.Save.OptimizedPalette = True

   codecs.Save(image, outStreamRaw, RasterImageFormat.Raw, 0)

   ' Save information about this image so we can use it to load the RAW file
   myRawData = New RawData()
   myRawData.Width = image.Width
   myRawData.Height = image.Height
   myRawData.BitsPerPixel = image.BitsPerPixel
   myRawData.ViewPerspective = image.ViewPerspective
   myRawData.Order = image.Order
   myRawData.XResolution = image.XResolution
   myRawData.YResolution = image.YResolution
   myRawData.Offset = 0
   myRawData.Padding = True
   myRawData.ReverseBits = True

   ' We dont need the image anymore
   image.Dispose()

   ' Now load this RAW image back

   ' First subscribe to the LoadInformation event so we can set the image information
   AddHandler codecs.LoadInformation, AddressOf codecs_LoadInformation

   ' Load the image
   image = codecs.Load(outStreamRaw)

   ' Unsubscribe from the event
   RemoveHandler codecs.LoadInformation, AddressOf codecs_LoadInformation

   ' save it as bmp for testing
   codecs.Save(image, outStreamBmp, RasterImageFormat.Bmp, 0)

   ' Clean up
   image.Dispose()
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterCodecs Class
RasterCodecs Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.