Recent

Author Topic: GDI+ Canvas  (Read 430 times)

CynicRus

  • Jr. Member
  • **
  • Posts: 62
GDI+ Canvas
« on: January 09, 2026, 10:29:06 pm »
Hello everyone! I decided to share with the community the gdiplus_canvas unit – a GDI+-based canvas implementation for Free Pascal and Lazarus. The TGDIPlusCanvas class inherits from TFPCustomCanvas, ensuring high compatibility with existing LCL code.

The module implements a complete set of graphical primitives: lines, Bézier curves, rectangles, ellipses, arcs, pies, polygons, as well as advanced text operations, including precise measurement and rendering with various alignment styles. Notably, it includes built-in support for gradient fills, complex brushes (including textured), path operations (GpPath), and clipping regions. All drawing operations use anti-aliasing for lines and text by default, significantly improving visual quality.

Technically, the module provides thread safety through locking mechanisms (Lock/Unlock), resource caching (fonts, text measurement) for performance, and several initialization options: from an existing HDC, from an HBITMAP, or by creating its own off-screen buffer of a specified size. The AutoRedraw property allows automatic copying of the off-screen buffer content to the target device context. The module fully integrates into the Lazarus ecosystem, supporting standard OnChange/OnChanging events and TCanvas-compatible properties like Pen, Brush, Font, and CopyMode.

To use this module, GDI+ must be available on the system (present in all modern Windows versions) and Free Pascal/Lazarus must be installed. The module is distributed under an FPC-compatible license and can be used in both commercial and open-source projects. It is recommended for tasks requiring high-quality graphics rendering: charting, scientific visualization, custom UI elements, and any drawing where the capabilities of the standard TCanvas are insufficient.

Unit code: https://gist.github.com/CynicRus/8b31691e88b6354710419a58a80527db
Test code: https://gist.github.com/CynicRus/71ad086e3abf60ce5fca14a24f486a8f

The license is free, use it as you wish.


Boleeman

  • Hero Member
  • *****
  • Posts: 1102
Re: GDI+ Canvas
« Reply #1 on: January 09, 2026, 11:34:06 pm »
Thanks CynicRus for providing the graphics GDI+ Canvas rendering available to Lazarus forum members.
I like playing around with all graphics related things.
Reminds me a bit of PlutoVG is a standalone 2D vector graphics library in C.
I played around with the Lazarus bindings for it last year.

I tried it out but I get the error

gdiplus_canvas.pas(8,30) Error: Cannot find gdipapi used by gdiplus_canvas of the Project Inspector.

I played around and eventually found the missing units. Also was a small error (needed to change to Render := TGDIPlusCanvas.Create(PaintBox1.Canvas.Handle);   ).

Attached below is a working version.


« Last Edit: January 10, 2026, 02:53:46 am by Boleeman »

 

TinyPortal © 2005-2018