Leadtools.Converters Namespace : RasterImageRenderer Class |
public static class RasterImageRenderer
'Declaration Public MustInherit NotInheritable Class RasterImageRenderer
'Usage Dim instance As RasterImageRenderer
public sealed static class RasterImageRenderer
function Leadtools.Converters.RasterImageRenderer()
public ref class RasterImageRenderer abstract sealed
The RasterImageRenderer class provides support for rendering a LEADTOOLS 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 Leadtools.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 RasterRenderBuffer.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 RasterRenderBuffer.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 RasterRenderBuffer.ToRasterImage helper method that allows you to convert the data inside a RasterRenderBuffer into a Leadtools.RasterImage object. This can be used to create a Leadtools.RasterImage from a Windows Runtime WriteableBitmap or HTML Canvas.
RasterImageRendererExamples.prototype.RasterImageRendererExample = function () { // Assume myCanvas is a Canvas on the page // Load the image var getFileAsync = Windows.ApplicationModel.Package.current.installedLocation.getFileAsync("Assets\\Ocr1.tif").then( function (getFileAsyncResult) { var codecs = new Leadtools.Codecs.RasterCodecs(); var leadStream = Leadtools.LeadStreamFactory.create(getFileAsyncResult); codecs.loadAsync(leadStream).then( function (rasterImage) { // Get the canvas var myCanvas = document.getElementById("myCanvas"); var context = myCanvas.getContext("2d"); // Get the ImageData var imageData = context.createImageData(myCanvas.width, myCanvas.height); var pixelData = imageData.data; var width = imageData.width; var height = imageData.height; // Render the image into the canvas using Scale 2 Gray var destRect = Leadtools.LeadRectHelper.create(0, 0, width, height); var properties = new Leadtools.Converters.RasterImageRenderProperties(); properties.renderDisplayMode = Leadtools.Converters.RasterImageRenderDisplayModeFlags.scaleToGray; var renderBuffer = Leadtools.Converters.RasterRenderBuffer.createFromHtmlImageData(imageData.width, imageData.height, pixelData); Leadtools.Converters.RasterImageRenderer.render(rasterImage, renderBuffer, srcRect, destRect, properties); // Set the image data back into the context context.putImageData(imageData, 0, 0); }); }); }
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(); }
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