Skip to content
forked from jingwood/d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.

License

Notifications You must be signed in to change notification settings

MomeProj/d2dlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

d2dlib

A .NET library that provides the hardware-accelerated high performance immediate drawing functionality via Direct2D API.

By using the graphics context to draw anything on windows form, control or draw in memory. The interface of graphics context is designed like standard Windows Form GDI+ graphics interface, it's easily use and user friendly.

Project Language Description Output DLL
d2dlib VC++ Wrapper host side library, calling Windows SDK and Direct2D API d2dlib.dll
d2dlibexport C# Wrapper client side library, export the interface provided from d2dlib d2dlibexport.dll
d2dlibexportwinform C# Provides the .NET classes used in windows form development, like D2DWinForm and D2DControl that use Direct2D hardware-acceleration rendering d2dlibwinform.dll

How to use

  1. Compile every projects or use the DLLs from the binary folder directly
  2. Add d2dlibexport.dll and d2dlibwinform.dll as application references
  3. Put d2dlib.dll in the Debug, Release or folder where application runs
  4. Make windows form or control inherited from D2DForm or D2DControl class
  5. Override OnRender(D2DGraphics g) method (do not override .NET OnPaint method)
  6. Draw anything inside OnRender method via the g context

Notice: The platform target of application project must be set to x86 when using default build configuration.

Basic rendering

Draw rectangle

var rect = new D2DRect(0, 0, 10, 10);
g.DrawEllipse(rect, D2DColor.Red);

Draw ellipse

var ellipse = new D2DEllipse(0, 0, 10, 10);
g.DrawEllipse(ellipse, D2DColor.Gray);

Draw text

protected override void OnRender(D2DGraphics g)
{
  g.DrawText("Hello World", D2DColor.Yellow, this.Font, 100, 200);
}

Using brush object

Solid color brush

var brush = Device.CreateSolidColorBrush(new D2DColor(1, 0, 0.5));
g.DrawEllipse(rect, brush);

Linear and radio 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),
  });

Draw bitmap

g.DrawBitmap(bmp, this.ClientRectangle);

Convert GDI+ bitmap to Direct2D bitmap for getting high performance rendering

// convert to Direct2D bitmap
var d2dbmp = Device.CreateBitmapFromGDIBitmap(gdiBitmap);

// draw Direct2D bitmap
g.DrawBitmap(d2dbmp, this.ClientRectangle);

Drawing on memory bitmap

Drawing on GDI+ bitmap

// create and draw on GDI+ bitmap
var gdiBmp = new Bitmap(1024, 1024);
using (Graphics g = Graphics.FromImage(gdiBmp))
{
  g.DrawString("This is GDI+ bitmap layer", new Font(this.Font.FontFamily, 48), Brushes.Black, 10, 10);
}

// draw memory bitmap on screen
g.DrawBitmap(gdiBmp, this.ClientRectangle);

Learn more about Bitmap. See Example code

Drawing on Direct2D memory bitmap

var bmpGraphics = this.Device.CreateBitmapGraphics(1024, 1024);
bmpGraphics.BeginRender();
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();

// draw this device bitmap on screen
g.DrawBitmap(bmpGraphics, this.ClientRectangle);

Note: When create a Direct2D Device bitmap, do not forget call BeginRender and EndRender method.

Using transform

By calling PushTransform and PopTransform to make a transform session.

g.PushTransform();

// rotate 45 degree
g.RotateTransform(45, centerPoint);

g.DrawBitmap(mybmp, rect);
g.PopTransform();

Examples

Fast images rendering Image Drawing Test See source code

Custom draw on memory bitmap Bitmap Custom Draw See source code

Star space simulation Star Space See source code

Subtitle rendering Subtitle See source code

Whiteboard App whiteboard
See source code

About

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 70.4%
  • C++ 22.4%
  • C 7.2%