public void CalculateConvertSizeExample() { RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); codecs.ThrowExceptionsOnInvalidImages = true; string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "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(); RasterCodecs.Shutdown(); } |