Canvas Class Tips

You can find few code snippets that show how to use TImagingCanvas class to draw onto images.

Canvas Creation And Updating

You can create canvas for both TBaseImage class descendants and TImageData structure.

    Imaging, ImagingTypes, ImagingClasses, ImagingCanvases;
    ImgData: TImageData;
    ImgObj: TSingleImage;
    Canvas: TImagingCanvas;
    // Load image to TImageData struct
    LoadImageFromFile('umajo.png', ImgData);
    // Load image to TSingleImage object
    ImgObj := TSingleImage.CreateFromFile('umajo2.png');
    // Create canvas for ImgData
    Canvas := TImagingCanvas.CreateForData(@ImgData);
    // Do some drawing
    // Resize image, you must then update canvas
    ResizeImage(ImgData, 600, 400, rfBicubic);
    // Draw some more
    // Recreate canvas, now for high level object
    // Do some drawing
    // Change image format, you must then update canvas
    ImgObj.Format := ifGray8;
    // Draw some more
    // Free all
Drawing Primitives And Filling

Now you will see how to draw lines, ellipses, and rectangles.

Canvas := TImagingCanvas.CreateForImage(Image);
    // You can set colors as 32bit or FP 128bit, they're automatically converted
    Canvas.FillColor32 := $FF808040;
    Canvas.PenColorFP := ColorFP(1.0, 0.6, 0.6, 1.0);
    // Set fill mode and pen mode to solid
    Canvas.PenMode := pmSolid;
    Canvas.FillMode := fmSolid;
    // Now set some pixels and draw lines using pen
    Canvas.Pixels32[200, 200] := $80FF0000;
    Canvas.VertLine(20, 0, Image.Height);
    Canvas.HorzLine(0, Image.Widht, 20);
    Canvas.Line(10, 10, 90, 100);
    // Draw filled rectangle and ellipse with outline
    // Draw outlined rectangle and ellipse
    Canvas.FillMode := fmClear;
    Canvas.Rectangle(Rect(0, 0, 50, 100));
    Canvas.Ellipse(Rect(0, 0, 50, 100));

    // Clear whole canvas (uses fill color)
    Canvas.FillColor32 := $FF000000;
Drawing Image

You can draw part of the canvas on another canvas. Blending with custom source and dest factors is supported with some best known combinations predefined (like alpha blending). Stretching part of the canvas is also possible with optional filtering (bilinear, bicubic, nearest).

Canvas := TImagingCanvas.CreateForImage(Image);
    // Stretch part of canvas onto another one with alpha blending and bicubic filtering
    Canvas.StretchDrawAlpha(SrcRect, DestCanvas, DestRect, rfBicubic);
    // Draw part of canvas onto another one with custom combination of blend factors
    Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfDstColor, bfSrcAlpha);
    // Draw part of canvas onto another one with additive blending
    Canvas.DrawAdd(SrcRect, DestCanvas, DestX, DestY);
    // Draw part of canvas onto another one with additive blending (using factors)
    Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfOne, bfOne);
    // Stretch part of canvas onto another one with addtive blending and bilinear filtering (default param)
    Canvas.StretchDrawAdd(SrcRect, DestCanvas, DestRect);

Canvas class also allows you to apply linear and nonlinear filters and use point transforms. You can always use your own kernels and functions but there are also some predefined.

Canvas := TImagingCanvas.CreateForImage(Image);
    // 3x3 Gaussian blurr
    // 7x7 Median filter
    // Modify contrast and brightness
    Canvas.ModifyContrastBrightness(20, -10);
    // Adjust gamma for each color channel
    Canvas.GammaCorection(0.9, 1.3, 0.75);

Look at VCL Image Browser Demo and LCL Imager Demo for more Canvas examples. General info on canvas usage is in Using Canvas Class