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



Provides support for resizing images. Supported in Silverlight, Windows Phone 7

Object Model

RasterImageResize Class

Syntax

Visual Basic (Declaration) 
Public Class RasterImageResize 
Visual Basic (Usage)Copy Code
Dim instance As RasterImageResize
C# 
public class RasterImageResize 
C++/CLI 
public ref class RasterImageResize 

Example

This sample loads an image and uses the RasterImageResize class to resize it to half its size.

Visual BasicCopy Code
Public Sub RasterImageResizeExample()
      Dim codecs As RasterCodecs = New RasterCodecs()
      codecs.ThrowExceptionsOnInvalidImages = True

      Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImageResize.bmp")

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

      Dim resize As RasterImageResize = New RasterImageResize()

      ' Add Handler
      AddHandler resize.Resize, AddressOf resize_Resize

      ' We will resize to half the original size
      Dim destWidth As Integer = srcImage.Width \ 2
      Dim destHeight As Integer = srcImage.Height \ 2

      ' Start the resize process
      resize.Start(srcImage, destWidth, destHeight, srcImage.BitsPerPixel, srcImage.Order, srcImage.DitheringMethod, RasterSizeFlags.None, srcImage.GetPalette())

      ' Create the destination image

      Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, destWidth, destHeight, srcImage.BitsPerPixel, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), IntPtr.Zero, 0)

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

      ' get the rows for the resized image, one by one
      Dim row As Integer = 0
      Do While row < destImage.Height
             resize.ResizeBuffer(row, 0, buffer, 0, destImage.BytesPerLine)
         destImage.SetRow(row, buffer, 0, destImage.BytesPerLine)
         row += 1
      Loop

      resize.Stop()

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

      ' clean up
      srcImage.Dispose()
      destImage.Dispose()
   End Sub

   Private Sub resize_Resize(ByVal sender As Object, ByVal e As RasterImageResizeEventArgs)
      ' e.Row should ALWAYS be less than e.Image.Height
      If e.Row >= e.Image.Height Then
         e.Cancel = True ' abort the resize
         Return
      End If

      Dim buffer As Byte() = New Byte(e.Bytes - 1) {}
      e.Image.Access()
      e.Image.GetRowColumn(e.Row, e.Column, buffer, 0, e.Bytes)
      e.Image.Release()
      Marshal.Copy(buffer, 0, e.Buffer, e.Bytes)
      Console.WriteLine("{0}, {1}", e.Row, e.Column)
   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 RasterImageResizeExample()
   {
      RasterCodecs codecs = new RasterCodecs();
      codecs.ThrowExceptionsOnInvalidImages = true;

      string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
      string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_RasterImageResize.bmp");

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

      RasterImageResize resize = new RasterImageResize();

      // Add Handler
      resize.Resize += new EventHandler<RasterImageResizeEventArgs>(resize_Resize);

      // We will resize to half the original size
      int destWidth = srcImage.Width / 2;
      int destHeight = srcImage.Height / 2;

      // Start the resize process
      resize.Start(
         srcImage,
         destWidth,
         destHeight,
         srcImage.BitsPerPixel,
         srcImage.Order,
         srcImage.DitheringMethod,
         RasterSizeFlags.None,
         srcImage.GetPalette());

      // Create the destination image

      RasterImage destImage = new RasterImage(
         RasterMemoryFlags.Conventional,
         destWidth,
         destHeight,
         srcImage.BitsPerPixel,
         srcImage.Order,
         srcImage.ViewPerspective,
         srcImage.GetPalette(),
         IntPtr.Zero,
         0);

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

      // get the rows for the resized image, one by one
      for(int row = 0; row < destImage.Height; row++)
      {
         resize.ResizeBuffer(row, 0, buffer, 0, destImage.BytesPerLine);
         destImage.SetRow(row, buffer, 0, destImage.BytesPerLine);
      }

      resize.Stop();

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

      // clean up
      srcImage.Dispose();
      destImage.Dispose();
   }

   private void resize_Resize(object sender, RasterImageResizeEventArgs e)
   {
      // e.Row should ALWAYS be less than e.Image.Height
      if(e.Row >= e.Image.Height)
      {
         e.Cancel = true; // abort the resize
         return;
      }

      byte[] buffer = new byte[e.Bytes];
      e.Image.Access();
      e.Image.GetRowColumn(e.Row, e.Column, buffer, 0, e.Bytes);
      e.Image.Release();
      Marshal.Copy(buffer, 0, e.Buffer, e.Bytes);
      Console.WriteLine("{0}, {1}", e.Row, e.Column);
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
SilverlightCSharpCopy Code
public void RasterImageResizeExample(RasterImage srcImage, Stream destStream)
{
   RasterCodecs codecs = new RasterCodecs();
   codecs.ThrowExceptionsOnInvalidImages = true;
   RasterImageResize resize = new RasterImageResize();

   // Add Handler
   resize.Resize += new EventHandler<RasterImageResizeEventArgs>(resize_Resize);

   // We will resize to half the original size
   int destWidth = srcImage.Width / 2;
   int destHeight = srcImage.Height / 2;

   // Start the resize process
   resize.Start(
      srcImage,
      destWidth,
      destHeight,
      srcImage.BitsPerPixel,
      srcImage.Order,
      srcImage.DitheringMethod,
      RasterSizeFlags.None,
      srcImage.GetPalette());

   // Create the destination image

   RasterImage destImage = new RasterImage(
      RasterMemoryFlags.Conventional,
      destWidth,
      destHeight,
      srcImage.BitsPerPixel,
      srcImage.Order,
      srcImage.ViewPerspective,
      srcImage.GetPalette(),
      null,
      0);

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

   // get the rows for the resized image, one by one
   for (int row = 0; row < destImage.Height; row++)
   {
      resize.ResizeBuffer(row, 0, buffer, 0, destImage.BytesPerLine);
      destImage.SetRow(row, buffer, 0, destImage.BytesPerLine);
   }

   resize.Stop();

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

   // clean up
   srcImage.Dispose();
   destImage.Dispose();
}

private void resize_Resize(object sender, RasterImageResizeEventArgs e)
{
   // e.Row should ALWAYS be less than e.Image.Height
   if (e.Row >= e.Image.Height)
   {
      e.Cancel = true; // abort the resize
      return;
   }

   byte[] buffer = new byte[e.Bytes];
   e.Image.Access();
   e.Image.GetRowColumn(e.Row, e.Column, buffer, 0, e.Bytes);
   e.Image.Release();
   Buffer.BlockCopy(buffer, 0, e.BufferArray, 0, e.Bytes);
   Console.WriteLine("{0}, {1}", e.Row, e.Column);
}
SilverlightVBCopy Code
Public Sub RasterImageResizeExample(ByVal srcImage As RasterImage, ByVal destStream As Stream)
   Dim codecs As RasterCodecs = New RasterCodecs()
   codecs.ThrowExceptionsOnInvalidImages = True
   Dim resize As RasterImageResize = New RasterImageResize()

   ' Add Handler
   AddHandler resize.Resize, AddressOf resize_Resize

   ' We will resize to half the original size
   Dim destWidth As Integer = srcImage.Width / 2
   Dim destHeight As Integer = srcImage.Height / 2

   ' Start the resize process
   resize.Start(srcImage, destWidth, destHeight, srcImage.BitsPerPixel, srcImage.Order, srcImage.DitheringMethod, RasterSizeFlags.None, srcImage.GetPalette())

   ' Create the destination image

   Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, destWidth, destHeight, srcImage.BitsPerPixel, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), Nothing, 0)

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

   ' get the rows for the resized image, one by one
   Dim row As Integer = 0
   Do While row < destImage.Height
      resize.ResizeBuffer(row, 0, buffer, 0, destImage.BytesPerLine)
      destImage.SetRow(row, buffer, 0, destImage.BytesPerLine)
      row += 1
   Loop

   resize.Stop()

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

   ' clean up
   srcImage.Dispose()
   destImage.Dispose()
End Sub

Private Sub resize_Resize(ByVal sender As Object, ByVal e As RasterImageResizeEventArgs)
   ' e.Row should ALWAYS be less than e.Image.Height
   If e.Row >= e.Image.Height Then
      e.Cancel = True ' abort the resize
      Return
   End If

   Dim buffer As Byte() = New Byte(e.Bytes - 1){}
   e.Image.Access()
   e.Image.GetRowColumn(e.Row, e.Column, buffer, 0, e.Bytes)
   e.Image.Release()
   System.Buffer.BlockCopy(buffer, 0, e.BufferArray, 0, e.Bytes)
   Console.WriteLine("{0}, {1}", e.Row, e.Column)
End Sub

Remarks

This class supports changing the color order, bits per pixel and/or palette, during the resize operation.

Inheritance Hierarchy

System.Object
   Leadtools.RasterImageResize

Requirements

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

See Also