←Select platform

RasterImageRenderer Class

Summary

Renders a LEADTOOLS RasterImage into the surface of Windows Runtime WriteableBitmap or HTML Canvas objects.

Syntax

C#
VB
WinRT C#
C++
public static class RasterImageRenderer
Public MustInherit NotInheritable Class RasterImageRenderer
public sealed static class RasterImageRenderer
function Leadtools.Converters.RasterImageRenderer()
public ref class RasterImageRenderer abstract sealed

Remarks

The RasterImageRenderer class provides support for rendering a LEADTOOLS RasterImage into a generic buffer containing 32-bit image data. This buffer can be the surface of a Windows Runtime WriteableBitmap object or the image data of HTML Canvas. The source RasterImage can have any bits/pixel or color order supported by LEADTOOLS.

To render any portion of a raster image into a Windows Runtime WriteableBitmap:

  • Create a RasterRenderBuffer object passing the width, height and pixel buffer of the WriteableBitmap. You can use the helper CreateFromWriteableBitmap helper method to setup the buffer properties correctly.

  • Call any of the RasterImageRenderer.Render methods to render the image into the buffer

  • Finally, call the WriteableBitmap.Invalidate method to apply the new data into the bitmap.

To render any portion of a raster image into a HTML Canvas:

  • Obtain the 2d context of canvas and then create a new HTML ImageData object with the required size, using Context.createImageData.

  • Create a RasterRenderBuffer object passing the width, height and image data of the ImageData. You can use the helper CreateFromHtmlImageData helper method to setup the buffer properties correctly.

  • Call any of the RasterImageRenderer.Render methods to render the image into the buffer

  • Finally, call context.putImageData to apply the new data into the canvas context.

You can also use an instance of RasterImageRenderProperties and change any of its properties to control the speed and quality of the render operation. Such as performing scale to gray or resample rendering.

RasterRenderBuffer also contains the ToRasterImage helper method that allows you to convert the data inside a RasterRenderBuffer into a RasterImage object. This can be used to create a RasterImage from a Windows Runtime WriteableBitmap or HTML Canvas.

Example

This example will load a 1-bit TIF file and render it into a smaller WriteableBitmap with scale-to-gray scaling.

WinRT C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Converters; 
 
public async void RasterImageRendererExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
   string srcFileName = @"Assets\Ocr1.tif"; 
   string destFileName = @"Ocr1_ScaleToGray.png"; 
 
   // Load the image 
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName); 
   ILeadStream leadStream = LeadStreamFactory.Create(loadFile); 
   RasterImage image = await codecs.LoadAsync(leadStream); 
 
   // Create a writeable bitmap that fit inside 300 by 300 pixels keeping the image aspect ratio 
   LeadRect destRect = LeadRectHelper.Create(0, 0, 300, 300); 
   destRect = RasterImage.CalculatePaintModeRectangle( 
      image.ImageWidth, 
      image.ImageHeight, 
      destRect, 
      RasterPaintSizeMode.Fit, 
      RasterPaintAlignMode.Near, 
      RasterPaintAlignMode.Near); 
 
   WriteableBitmap bitmap = new WriteableBitmap(destRect.Width, destRect.Height); 
 
   // Create a RasterRenderBuffer object for the bitmap 
   RasterRenderBuffer renderBuffer = RasterRenderBuffer.CreateFromWriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.PixelBuffer); 
 
   // Render the image into the buffer using Scale to Gray 
   RasterImageRenderProperties properties = new RasterImageRenderProperties(); 
   properties.RenderDisplayMode = RasterImageRenderDisplayModeFlags.ScaleToGray; 
   RasterImageRenderer.Render(image, renderBuffer, destRect, properties); 
   bitmap.Invalidate(); 
 
   // No need for the image anymore 
   image.Dispose(); 
 
   // Save the writeable bitmap using PngEncoder 
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName, CreationCollisionOption.ReplaceExisting); 
   IRandomAccessStream stream = await saveFile.OpenAsync(FileAccessMode.ReadWrite); 
   BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); 
   Stream pixelStream = bitmap.PixelBuffer.AsStream(); 
   byte[] pixels = new byte[pixelStream.Length]; 
   await pixelStream.ReadAsync(pixels, 0, pixels.Length); 
   encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels); 
   await encoder.FlushAsync(); 
 
   // Clean up 
   codecs.Dispose(); 
} 

Requirements

Target Platforms

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
Click or drag to resize