LEADTOOLS (Leadtools assembly)
LEAD Technologies, Inc

RasterImage Class

Example 





Members 
The RasterImage class serves as a working area for image manipulation and conversion. LEADTOOLS functions use this class for accessing the image in memory and for maintaining the characteristics of the image. .NET support Silverlight support WinRT support
Object Model
RasterImage ClassRasterColor StructureRasterColor StructureLeadSize StructureLeadPoint StructureLeadSize StructureRasterImageMemoryInformation StructureRasterColor Structure
Syntax
'Declaration
 
<SerializableAttribute()>
Public Class RasterImage 
   Implements System.IDisposableSystem.Runtime.Serialization.ISerializable 
'Usage
 
Dim instance As RasterImage
function Leadtools.RasterImage()
Remarks

The RasterImage class is used to work with images defined by pixel data.

The RasterImage class contains methods and properties for dealing with images in memory. You can use this class to create images from scratch or load them from disk based files. Also, various other components of LEADTOOLS for .NET creates RasterImage objects from operations such as scanning and OCRing. The RasterImage class is the main LEADTOOLS object used when passing image data between different parts of the toolkit.

The RasterImage class lets you deal with individual rows or pixels of the image data. This class also contains methods and properties for the following:

The RasterImage class implements the System.Runtime.Serialization.ISerializable interface and thus supports standard .NET serialization. For more information and examples regarding serialization of an RasterImage object, refer to RasterImage Serialization.

The RasterImage class also implements the System.IDisposable interface, it is recommended that you follow the standard .NET dispose pattern when using the RasterImage class. For more information, refer to the System.IDisposable interface documentation in MSDN, the IsDisposed property and the RasterImage.Disposed event.

The LEADTOOLS RasterImage class supports storing images in memory in the following bits per pixel: 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 38 and 64. A 16-bpp image may be color or grayscale. If you are working with a 16-bit grayscale image. Support for 8-bit and 12-bit grayscale images is also provided.

The data of a RasterImage object can be stored in memory as uncompressed, RLE compressed or super compressed. Support for super compressing bitmaps is available only in the Document/Medical Imaging editions.

The RasterImage object can also store its data in disk-based swap files when conventional memory is not enough.

The RasterImage object can hold multiple pages with different sizes. The AddPage, AddPages, InsertPage and InsertPages methods allows adding new pages to an existing RasterImage.

The RemovePageAt, RemovePages and RemoveAllPages methods allows removing existing pages from a RasterImage object.

The ReplacePage and ReplacePages methods allows replace existing pages in a RasterImage object.

The PageCount property holds the total number of pages in a RasterImage object while the Page property allows you to change the current active page.

The current active page (The page indicated by the Page property) is used by default when accessing the data of a RasterImage object unless otherwise indicated.

The RasterImage object also contains a region of interest value that can be used to limit the portion available to update when using the image processing command. The region can be set using a geometric shape such as AddRectangleToRegion and AddEllipseToRegion or with image data attributes such as AddColorToRegion and AddMaskToRegion.

Example
 
Public Sub RasterImageExample()
      Dim codecs As RasterCodecs = New RasterCodecs()

      Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim destFileName1 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImage1.bmp")
      Dim destFileName2 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImage2.bmp")

      ' Load the image
      Dim srcImage As RasterImage = codecs.Load(srcFileName)

      ' Creates a new image in memory with same dimensions as the source image
      Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, srcImage.Width, srcImage.Height, srcImage.BitsPerPixel, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), IntPtr.Zero, 0)

      ' Copy the data from the source image to the destination image
      srcImage.Access()
      destImage.Access()

      Dim buffer As Byte() = New Byte(srcImage.BytesPerLine - 1) {}

      Dim y As Integer = 0
      Do While y < srcImage.Height
         srcImage.GetRow(y, buffer, 0, buffer.Length)
         destImage.SetRow(y, buffer, 0, buffer.Length)
         y += 1
      Loop

      destImage.Release()
      srcImage.Release()

      ' We do not need the source image anymore
      srcImage.Dispose()

      ' save the destination image
      codecs.Save(destImage, destFileName1, RasterImageFormat.Bmp, 24)

      ' perform image processing on the image

      Dim flipCmd As FlipCommand = New FlipCommand()
      flipCmd.Horizontal = False
      flipCmd.Run(destImage)

      ' save it
      codecs.Save(destImage, destFileName2, RasterImageFormat.Bmp, 24)

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

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void RasterImageExample()
   {
      RasterCodecs codecs = new RasterCodecs();

      string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
      string destFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImage1.bmp");
      string destFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImage2.bmp");

      // Load the image
      RasterImage srcImage = codecs.Load(srcFileName);

      // Creates a new image in memory with same dimensions as the source image
      RasterImage destImage = new RasterImage(
         RasterMemoryFlags.Conventional,
         srcImage.Width,
         srcImage.Height,
         srcImage.BitsPerPixel,
         srcImage.Order,
         srcImage.ViewPerspective,
         srcImage.GetPalette(),
         IntPtr.Zero,
         0);

      // Copy the data from the source image to the destination image
      srcImage.Access();
      destImage.Access();

      byte[] buffer = new byte[srcImage.BytesPerLine];

      for(int y = 0; y < srcImage.Height; y++)
      {
         srcImage.GetRow(y, buffer, 0, buffer.Length);
         destImage.SetRow(y, buffer, 0, buffer.Length);
      }

      destImage.Release();
      srcImage.Release();

      // We do not need the source image anymore
      srcImage.Dispose();

      // save the destination image
      codecs.Save(destImage, destFileName1, RasterImageFormat.Bmp, 24);

      // perform image processing on the image

      FlipCommand flipCmd = new FlipCommand();
      flipCmd.Horizontal = false;
      flipCmd.Run(destImage);

      // save it
      codecs.Save(destImage, destFileName2, RasterImageFormat.Bmp, 24);

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

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
RasterImageExamples.prototype.RasterImageExample = function () {
    Tools.SetLicense();
    with (Leadtools) {
        with (Leadtools.Codecs) {
            var codecs = new RasterCodecs();

            var srcFileName = "Assets\\Image1.cmp";
            var destFileName1 = "Image1_RasterImage1.bmp";
            var destFileName2 = "Image1_RasterImage2.bmp";
            var destImage;
            // Load the image
            // Load the image
            return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (loadFile) {

                return codecs.loadAsync(LeadStreamFactory.create(loadFile));
            })
            .then(function (srcImage) {
                console.assert(srcImage.bitsPerPixel == 24, "srcImage.bitsPerPixel == 24");

                // Creates a new image in memory with same dimensions as the source image
                destImage = new RasterImage(
                   RasterMemoryFlags.conventional,
                   srcImage.width,
                   srcImage.height,
                   srcImage.bitsPerPixel,
                   srcImage.order,
                   srcImage.viewPerspective,
                   srcImage.getPalette());

                // Copy the data from the source image to the destination image
                srcImage.accessData();
                destImage.accessData();

                var buffer = new Array(srcImage.BytesPerLine);

                for (var y = 0; y < srcImage.height; y++) {
                    var number = srcImage.getRow(y, buffer, 0, srcImage.BytesPerLine);
                    destImage.setRow(y, buffer, number, srcImage.BytesPerLine);
                }

                destImage.releaseData();
                srcImage.releaseData();

                // We do not need the source image anymore
                srcImage.close();

                // save the destination image
                return Tools.AppLocalFolder().createFileAsync(destFileName1)})
                .then(function (saveFile) {
                    return codecs.saveAsync(destImage, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
                })
                .then(function () {

                    // perform image processing on the image

                    var flipCmd = new Leadtools.ImageProcessing.FlipCommand();
                    flipCmd.horizontal = false;
                    flipCmd.run(destImage);

                    // save it
                    return Tools.AppLocalFolder().createFileAsync(destFileName2)
                })
                        .then(function (saveFile) {
                            return codecs.saveAsync(destImage, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
                        })
                        .then(function () {

                            // Clean up
                            destImage.close();
                            codecs.close();
                        });
        }
    }
}
[TestMethod]
public async Task RasterImageExample()
{
   RasterCodecs codecs = new RasterCodecs();
   string srcFileName = @"Assets\Image1.cmp";
   string destFileName1 = @"Image1_RasterImage1.bmp";
   string destFileName2 = @"Image1_RasterImage2.bmp";

   // Load the image
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage srcImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 24, CodecsLoadByteOrder.Bgr, 1, 1);
   Assert.IsTrue(srcImage.BitsPerPixel == 24);

   // Creates a new image in memory with same dimensions as the source image
   RasterImage destImage = new RasterImage(
      RasterMemoryFlags.Conventional,
      srcImage.Width,
      srcImage.Height,
      srcImage.BitsPerPixel,
      srcImage.Order,
      srcImage.ViewPerspective,
      srcImage.GetPalette());

   // Copy the data from the source image to the destination image
   srcImage.AccessData();
   destImage.AccessData();

   Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer((uint)srcImage.BytesPerLine);

   for(int y = 0; y < srcImage.Height; y++)
   {
      srcImage.GetRow(y, buffer, 0, (int)buffer.Length);
      destImage.SetRow(y, buffer, 0, (int)buffer.Length);
   }

   destImage.ReleaseData();
   srcImage.ReleaseData();

   // We do not need the source image anymore
   srcImage.Dispose();

   // save the destination image
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName1);
   await codecs.SaveAsync(destImage, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);

   // perform image processing on the image

   FlipCommand flipCmd = new FlipCommand();
   flipCmd.Horizontal = false;
   flipCmd.Run(destImage);

   // save it
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName2);
   await codecs.SaveAsync(destImage, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);

   // Clean up
   destImage.Dispose();
   codecs.Dispose();
}
public void RasterImageExample(RasterImage srcImage, Stream outputStream1, Stream outputStream2)
{
   RasterCodecs codecs = new RasterCodecs();
   // Creates a new image in memory with same dimensions as the source image
   RasterImage destImage = new RasterImage(
      RasterMemoryFlags.Conventional,
      srcImage.Width,
      srcImage.Height,
      srcImage.BitsPerPixel,
      srcImage.Order,
      srcImage.ViewPerspective,
      srcImage.GetPalette(),
      null,
      0);

   // Copy the data from the source image to the destination image
   srcImage.Access();
   destImage.Access();

   byte[] buffer = new byte[srcImage.BytesPerLine];

   for (int y = 0; y < srcImage.Height; y++)
   {
      srcImage.GetRow(y, buffer, 0, buffer.Length);
      destImage.SetRow(y, buffer, 0, buffer.Length);
   }

   destImage.Release();
   srcImage.Release();

   // We do not need the source image anymore
   srcImage.Dispose();

   // save the destination image
   codecs.Save(destImage, outputStream1, RasterImageFormat.Bmp, 0);

   // perform image processing on the image

   FlipCommand flipCmd = new FlipCommand();
   flipCmd.Horizontal = false;
   flipCmd.Run(destImage);

   // save it
   codecs.Save(destImage, outputStream2, RasterImageFormat.Bmp, 0);

   // Clean up
   destImage.Dispose();
}
Public Sub RasterImageExample(ByVal srcImage As RasterImage, ByVal outputStream1 As Stream, ByVal outputStream2 As Stream)
   Dim codecs As RasterCodecs = New RasterCodecs()
   ' Creates a new image in memory with same dimensions as the source image
   Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, srcImage.Width, srcImage.Height, srcImage.BitsPerPixel, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), Nothing, 0)

   ' Copy the data from the source image to the destination image
   srcImage.Access()
   destImage.Access()

   Dim buffer As Byte() = New Byte(srcImage.BytesPerLine - 1){}

   Dim y As Integer = 0
   Do While y < srcImage.Height
      srcImage.GetRow(y, buffer, 0, buffer.Length)
      destImage.SetRow(y, buffer, 0, buffer.Length)
      y += 1
   Loop

   destImage.Release()
   srcImage.Release()

   ' We do not need the source image anymore
   srcImage.Dispose()

   ' save the destination image
   codecs.Save(destImage, outputStream1, RasterImageFormat.Bmp, 0)

   ' perform image processing on the image

   Dim flipCmd As FlipCommand = New FlipCommand()
   flipCmd.Horizontal = False
   flipCmd.Run(destImage)

   ' save it
   codecs.Save(destImage, outputStream2, RasterImageFormat.Bmp, 0)

   ' Clean up
   destImage.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

RasterImage Members
Leadtools Namespace
RasterImage and GDI/GDI+
RasterImage and WPF/Silverlight

 

 


Products | Support | Contact Us | Copyright Notices

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