d2dlib

repository·master·Indexed 42 days ago

https://github.com/jingwood/d2dlib

A .NET library for hardware-accelerated, high-performance immediate mode rendering via Direct2D. It mimics the standard Windows Forms graphics interface, providing D2DForm and D2DControl classes for rendering via the OnRender method. Supports drawing primitives, text, brushes, and high-performance bitmap conversion from GDI+.

Tokens
1.8K
Snippets
6
Records
8
Agent score
4%

What's inside d2dlib

  1. Draw and convert bitmaps

    master

    d2dlib supports high-performance bitmap rendering. You can draw standard GDI+ bitmaps directly, or convert them to Direct2D bitmaps for better performance.

    Convert GDI+ to Direct2D

    To get high-performance rendering, convert your System.Drawing.Bitmap using Device.CreateBitmapFromGDIBitmap(gdiBitmap) before drawing.

    Drawing on Direct2D Memory Bitmaps

    You can create a dedicated Direct2D memory bitmap (device bitmap) for custom drawing. When using CreateBitmapGraphics, you must wrap your drawing operations between BeginRender() and EndRender() calls.

    // 1. Convert GDI+ bitmap to Direct2D bitmap for high performance
    var d2dbmp = Device.CreateBitmapFromGDIBitmap(gdiBitmap);
    g.DrawBitmap(d2dbmp, this.ClientRectangle);
    
    // 2. Drawing on a Direct2D memory bitmap
    var bmpGraphics = this.Device.CreateBitmapGraphics(1024, 1024);
    bmpGraphics.BeginRender(); // Required
    bmpGraphics.FillRectangle(170, 790, 670, 80, new D2DColor(0.4f, D2DColor.Black));
    bmpGraphics.DrawText("This is Direct2D device bitmap", D2DColor.Goldenrod, this.Font, 180, 800);
    bmpGraphics.EndRender(); // Required
    
    // Draw the resulting device bitmap to the screen
    g.DrawBitmap(bmpGraphics, this.ClientRectangle);
  2. How to get started with d2dlib rendering

    master

    To use d2dlib in a Windows Forms application, follow these steps:

    1. Inherit your Windows Form or Control from the D2DForm or D2DControl classes provided by d2dwinform.dll.
    2. Override the OnRender(D2DGraphics g) method. Do not override the standard .NET OnPaint method.
    3. Use the provided D2DGraphics g context inside OnRender to perform your drawing operations.
  3. Install d2dlib via NuGet

    master

    You can install the d2dlib library using NuGet. For standard installations, use the default package. For 64-bit specific environments, use the x64 package.

    Important: Because Direct2D is a platform-associated API, you must set your project's Platform target to either x86 or x64 in the project settings. It will not work with Any CPU if the target is not explicitly defined.

    # Standard installation
    install-package unvell.d2dlib
    
    # x64 platform installation
    install-package unvell.d2dlib-x64
  4. Configure project architecture for d2dlib

    master

    Depending on your target .NET version, you must configure your project architecture to ensure compatibility with the library:

    For .NET Core or .NET Framework 4.5 and later

    Enable the "Prefer 32-bit" option. This can be found on the 'Build' page of your project settings.

    For .NET Framework 4 and earlier

    Change your project's build configuration from 'Any CPU' to 'x86' architecture.

  5. Get started with d2dlib drawing

    master

    To begin using d2dlib for rendering, follow these two steps:

    1. Inherit from d2dlib classes: Ensure your Windows Form or Control inherits from either the D2DForm or D2DControl class.
    2. Implement drawing logic: Override the OnRender method to perform your drawing operations.

    Important: Do not override the OnPaint method; use OnRender instead.

    // Example pattern
    public class MyD2DForm : D2DForm
    {
        protected override void OnRender(Graphics graphics)
        {
            // Your drawing code here
            base.OnRender(graphics);
        }
    }
  6. Apply transformations using PushTransform and PopTransform

    master

    To apply transformations like rotation or scaling to a specific set of drawing operations, use a transform session. This prevents the transformation from affecting subsequent drawing calls outside the session.

    1. Call g.PushTransform() to start the session.
    2. Apply transformations (e.g., g.RotateTransform(angle, centerPoint)).
    3. Perform drawing.
    4. Call g.PopTransform() to restore the previous coordinate state.
    g.PushTransform();
    
    // rotate 45 degree
    g.RotateTransform(45, centerPoint);
    
    g.DrawBitmap(mybmp, rect);
    
    g.PopTransform();
  7. Use brushes for advanced coloring

    master

    While simple shapes can take a D2DColor, you can use Brush objects for more complex fills and gradients. Brushes are created via the Device object.

    • Solid Color Brush: Created via Device.CreateSolidColorBrush(D2DColor color).
    • Linear Gradient Brush: Created via Device.CreateLinearGradientBrush(D2DPoint start, D2DPoint end, D2DGradientStop[] stops).
    • Radial Gradient Brush: (Supported via the device interface).
    // Solid color brush
    var brush = Device.CreateSolidColorBrush(new D2DColor(1, 0, 0.5));
    g.DrawEllipse(rect, brush);
    
    // Linear gradient brush
    var brush = Device.CreateLinearGradientBrush(new D2DPoint(0, 0), new D2DPoint(200, 100),
      new D2DGradientStop[] {
        new D2DGradientStop(0, D2DColor.White),
        new D2DGradientStop(0.5, D2DColor.Green),
        new D2DGradientStop(1, D2DColor.Black),
      });
  8. Draw basic shapes and text

    master

    The D2DGraphics object provides methods similar to the standard Windows Forms graphics interface for drawing primitives and text.

    • Rectangles: Use DrawRectangle(D2DRect rect, D2DColor color).
    • Ellipses: Use DrawEllipse(D2DEllipse ellipse, D2DColor color).
    • Text: Use DrawText(string text, D2DColor color, Font font, float x, float y).
    protected override void OnRender(D2DGraphics g)
    {
      // Draw a rectangle
      var rect = new D2DRect(0, 0, 10, 10);
      g.DrawRectangle(rect, D2DColor.Red);
    
      // Draw an ellipse
      var ellipse = new D2DEllipse(0, 0, 10, 10);
      g.DrawEllipse(ellipse, D2DColor.Gray);
    
      // Draw text
      g.DrawText("Hello World", D2DColor.Yellow, this.Font, 100, 200);
    }