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



inWidth
Width in pixels of the input image data.
inBitsPerPixel
BitsPerPixel of the input image data.
outWidth
Width in pixels of the desired output image data.
outBitsPerPixel
BitsPerPixel of the desired output image data.
inWidth
Width in pixels of the input image data.
inBitsPerPixel
BitsPerPixel of the input image data.
outWidth
Width in pixels of the desired output image data.
outBitsPerPixel
BitsPerPixel of the desired output image data.
Calculates the size required for output buffer. Supported in Silverlight, Windows Phone 7

Syntax

Visual Basic (Declaration) 
Public Shared Function CalculateConvertSize( _
   ByVal inWidth As Integer, _
   ByVal inBitsPerPixel As Integer, _
   ByVal outWidth As Integer, _
   ByVal outBitsPerPixel As Integer _
) As Integer
Visual Basic (Usage)Copy Code
Dim inWidth As Integer
Dim inBitsPerPixel As Integer
Dim outWidth As Integer
Dim outBitsPerPixel As Integer
Dim value As Integer
 
value = RasterBufferConverter.CalculateConvertSize(inWidth, inBitsPerPixel, outWidth, outBitsPerPixel)
C# 
public static int CalculateConvertSize( 
   int inWidth,
   int inBitsPerPixel,
   int outWidth,
   int outBitsPerPixel
)
C++/CLI 
public:
static int CalculateConvertSize( 
   int inWidth,
   int inBitsPerPixel,
   int outWidth,
   int outBitsPerPixel
) 

Parameters

inWidth
Width in pixels of the input image data.
inBitsPerPixel
BitsPerPixel of the input image data.
outWidth
Width in pixels of the desired output image data.
outBitsPerPixel
BitsPerPixel of the desired output image data.

Return Value

The size in bytes of the required output buffer.

Example

This example loads an image at 24 bits per pixel, and creates a new image at 4 bits per pixel, and uses the Convert method to convert data from 24 bits per pixel to 4 bits per pixel.

Visual BasicCopy Code
Public Sub CalculateConvertSizeExample()
      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_4bits.bmp")

      ' Load the image, at 24 bit per pixel.
      Dim srcImage As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)
      Debug.Assert(srcImage.BitsPerPixel = 24)

      ' Create a new 4-bit image.
      Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, srcImage.Width, srcImage.Height, 4, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), IntPtr.Zero, 0)
      Debug.Assert(destImage.BitsPerPixel = 4)

      Dim bufferSize As Integer = RasterBufferConverter.CalculateConvertSize(srcImage.Width, srcImage.BitsPerPixel, destImage.Width, destImage.BitsPerPixel)

      ' Allocate the buffer in unmanaged memory
      Dim buffer As IntPtr = Marshal.AllocHGlobal(bufferSize)

      ' Process each row from srcImage to destImage.
      srcImage.Access()
      destImage.Access()

      Dim i As Integer = 0
      Do While i < srcImage.Height
         srcImage.GetRow(i, buffer, srcImage.BytesPerLine)
         RasterBufferConverter.Convert(buffer, srcImage.Width, srcImage.BitsPerPixel, destImage.BitsPerPixel, srcImage.Order, destImage.Order, Nothing, Nothing, 0, 8, 0, RasterConvertBufferFlags.None)
         destImage.SetRow(i, buffer, destImage.BytesPerLine)
         i += 1
      Loop

      destImage.Release()
      srcImage.Release()

      ' Save the destination image back to disk
      codecs.Save(destImage, destFileName, RasterImageFormat.Bmp, 4)

      ' Clean up
      Marshal.FreeHGlobal(buffer)
      srcImage.Dispose()
      destImage.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 CalculateConvertSizeExample()
   {
      RasterCodecs codecs = new RasterCodecs();
      codecs.ThrowExceptionsOnInvalidImages = true;

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

      // Load the image, at 24 bit per pixel.
      RasterImage srcImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1);
      Debug.Assert(srcImage.BitsPerPixel == 24);

      // Create a new 4-bit image.
      RasterImage destImage = new RasterImage(
         RasterMemoryFlags.Conventional,
         srcImage.Width,
         srcImage.Height,
         4,
         srcImage.Order,
         srcImage.ViewPerspective,
         srcImage.GetPalette(),
         IntPtr.Zero,
         0);
      Debug.Assert(destImage.BitsPerPixel == 4);

      int bufferSize = RasterBufferConverter.CalculateConvertSize(
         srcImage.Width,
         srcImage.BitsPerPixel,
         destImage.Width,
         destImage.BitsPerPixel);

      // Allocate the buffer in unmanaged memory
      IntPtr buffer = Marshal.AllocHGlobal(bufferSize);

      // Process each row from srcImage to destImage.
      srcImage.Access();
      destImage.Access();

      for(int i = 0; i < srcImage.Height; i++)
      {
         srcImage.GetRow(i, buffer, srcImage.BytesPerLine);
         RasterBufferConverter.Convert(
            buffer,
            srcImage.Width,
            srcImage.BitsPerPixel,
            destImage.BitsPerPixel,
            srcImage.Order,
            destImage.Order,
            null,
            null,
            0,
            8,
            0,
            RasterConvertBufferFlags.None);
         destImage.SetRow(i, buffer, destImage.BytesPerLine);
      }

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

      // Save the destination image back to disk
      codecs.Save(destImage, destFileName, RasterImageFormat.Bmp, 4);

      // Clean up
      Marshal.FreeHGlobal(buffer);
      srcImage.Dispose();
      destImage.Dispose();
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
SilverlightCSharpCopy Code
public void CalculateConvertSizeExample(RasterImage srcImage, Stream destStream)
{
   // srcImage should be 24 bits per pixel.
   RasterCodecs codecs = new RasterCodecs();
   codecs.ThrowExceptionsOnInvalidImages = true;
   // Create a new 4-bit image.
   RasterImage destImage = new RasterImage(
      RasterMemoryFlags.Conventional,
      srcImage.Width,
      srcImage.Height,
      4,
      srcImage.Order,
      srcImage.ViewPerspective,
      srcImage.GetPalette(),
      null,
      0);
   Debug.Assert(destImage.BitsPerPixel == 4);

   int bufferSize = RasterBufferConverter.CalculateConvertSize(
      srcImage.Width,
      srcImage.BitsPerPixel,
      destImage.Width,
      destImage.BitsPerPixel);

   // Allocate the buffer
   byte[] buffer = new byte[bufferSize];

   // Process each row from srcImage to destImage.
   srcImage.Access();
   destImage.Access();

   for(int i = 0; i < srcImage.Height; i++)
   {
      srcImage.GetRow(i, buffer, 0, srcImage.BytesPerLine);
      RasterBufferConverter.Convert(
         buffer,
         0,
         srcImage.Width,
         srcImage.BitsPerPixel,
         destImage.BitsPerPixel,
         srcImage.Order,
         destImage.Order,
         null,
         null,
         0,
         8,
         0,
         RasterConvertBufferFlags.None);
      destImage.SetRow(i, buffer, 0, destImage.BytesPerLine);
   }

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

   // Save the destination image back to disk
   codecs.Save(destImage, destStream, RasterImageFormat.Bmp, 4);

   // Clean up
   srcImage.Dispose();
   destImage.Dispose();
}
SilverlightVBCopy Code
Public Sub CalculateConvertSizeExample(ByVal srcImage As RasterImage, ByVal destStream As Stream)
   ' srcImage should be 24 bits per pixel.
   Dim codecs As RasterCodecs = New RasterCodecs()
   codecs.ThrowExceptionsOnInvalidImages = True
   ' Create a new 4-bit image.
   Dim destImage As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, srcImage.Width, srcImage.Height, 4, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), Nothing, 0)
   Debug.Assert(destImage.BitsPerPixel = 4)

   Dim bufferSize As Integer = RasterBufferConverter.CalculateConvertSize(srcImage.Width, srcImage.BitsPerPixel, destImage.Width, destImage.BitsPerPixel)

   ' Allocate the buffer
   Dim buffer As Byte() = New Byte(bufferSize - 1){}

   ' Process each row from srcImage to destImage.
   srcImage.Access()
   destImage.Access()

   Dim i As Integer = 0
   Do While i < srcImage.Height
      srcImage.GetRow(i, buffer, 0, srcImage.BytesPerLine)
      RasterBufferConverter.Convert(buffer, 0, srcImage.Width, srcImage.BitsPerPixel, destImage.BitsPerPixel, srcImage.Order, destImage.Order, Nothing, Nothing, 0, 8, 0, RasterConvertBufferFlags.None)
      destImage.SetRow(i, buffer, 0, destImage.BytesPerLine)
      i += 1
   Loop

   destImage.Release()
   srcImage.Release()

   ' Save the destination image back to disk
   codecs.Save(destImage, destStream, RasterImageFormat.Bmp, 4)

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

Remarks

You can use this method to calculate the required size of the output buffer for image data conversions.

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