17

GDI+ is very slow, almost entirely software whereas GDI is highly hardware accelerated. GDI+ is what the Graphics class uses on WinForms and it's just too slow.

Has anyone made a .NET GDI library so we can have the speed?

[EDIT] Many people are recommending OpenGL/DirectX. A requirement of mine is client compatibility especially remote desktop. AFAIK remote desktop does not support OGL/DirectX out of the box.[/EDIT]

Joe
  • 964
  • 1
  • 10
  • 27
  • 2
    GDI+ utilizes GDI. If you need to use GDI directly, use c or c++. – Michael Todd Jun 17 '11 at 21:14
  • 6
    What're you trying to do that GDI+ slows you down so much? I've always found it plenty fast except for drawing bitmaps and in that case you can drop to PInvoke BitBlt. – Paul Sasik Jun 17 '11 at 21:15
  • 3
    Can you be more specific on exactly what parts of GDI are slowing you down? – Brandon Moretz Jun 17 '11 at 21:43
  • @Paul Sasik: Yep, or, if you need direct access to the image in memory, just call LockBits and get a pointer. – Ed S. Jun 24 '11 at 22:39
  • Specifically I'm drawing grids and possibly charts (for financial systems) The typical client has 4 monitors and a lot of my windows are full screen on 2 - 4 of their monitors – Joe Apr 12 '12 at 21:46

5 Answers5

27

Text rendering in GDI+ is slower than GDI. Microsoft realized this after .NET 1.1.

That is why .NET 2.0 contains a new TextRenderer class that wraps GDI DrawText. It has two static methods:

In .NET 2.0, all WinForm controls were converted to use TextRenderer, instead of:

  • Graphics.MeasureString
  • Graphics.DrawString

(provided you turn off UseCompatibleTextRendering)


Drawing a Bitmap is also slow in GDI+, that is why you use CachedBitmap. It draws very speedy.

A CachedBitmap object stores a bitmap in a format that is optimized for display on a particular device. To display a cached bitmap, call the Graphics::DrawCachedBitmap method.

graphics.DrawCachedBitmap(bitmap, 0, 0);

See also

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Some people here: http://stackoverflow.com/questions/71374/fastest-api-for-rendering-text-in-windows-forms Say TextRenderer is slower on Vista+. What's your experience say? I only care about Vista/7/+ – Joe Jun 25 '11 at 21:29
  • Microsoft guy, Chris Jackson, wrote an excellent post where he benchmarked the two: http://blogs.msdn.com/b/cjacks/archive/2006/05/19/gdi-vs-gdi-text-rendering-performance.aspx – Ian Boyd Jun 30 '11 at 13:41
  • 3
    I wasn't able to find DrawCachedBitmap in any C# library. So if anyone is interested I made a small managed C++ wrapper to use CachedBitmap from C# (https://github.com/svejdo1/CachedBitmap) – Ondrej Svejdar Aug 07 '14 at 12:17
7

Have you checked out SlimDX? It has a managed wrapper around Direct2D, which is a fully hardware accelerated 2d API and also mixes well with traditional GDI.

Edit:

I also found this library you might find interesting: SharpDX

"SharpDX is intended to be used as an alternative managed DirectX framework. The API is generated automatically from DirectX SDK headers, with AnyCpu target, meaning that you can run your application on x86 and x64 platform, without recompiling your project or installing assemblies into the GAC. SharpDX is also the fastest Managed-DirectX implementation."

This also has support for Direct2D.

Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
  • i've not heard of SlimDX or SharpDX. But i was going to mention Direct2D. The downside is that you have to create an entire control library from scratch. WPF should have been the hardware accelerated option; but it's slow to launch, sluggish to animate and difficult to learn. – Ian Boyd Jun 24 '11 at 23:05
  • Wow SharpDX looks really nice. It does not look like it works over RDP :( – Joe Apr 12 '12 at 21:48
2

You can try and move to WPF.
I did so because WinForms draw slowly.
The transition is very hard, especially for old-fashioned code-ers like me.

But WinForms cannot be beaten at startup speed and RAM.
WPF starts slowly and takes lots of RAM.

Vercas
  • 8,931
  • 15
  • 66
  • 106
  • 1
    I have experience with WPF and have built a few WPF windows. I noticed WPF also has poor performance especially on machines with low-end/integrated graphics (such as my work machine) The problem becomes serious (both on GDI+ and WPF) when the window size is big or maximized. – Joe Jun 17 '11 at 21:28
  • 1
    Also, I agree the transition is NOT easy and it's painful. I still find myself using WPF like WinForms, that is- not using fancy binding and instead using a lot of code behind instead to do a lot of the work that can be automagitically done- not sure if that effects the performance much. – Joe Jun 17 '11 at 21:31
  • 1
    There's been a lot of screaming about the performance of WPF (A quad-core nVidia desktop can't animate as smoothly as an embedded iPhone device?) http://fixwpf.org/ – Ian Boyd Jun 24 '11 at 23:03
2

The Microsoft API Code Pack (Archived Feb. 2010) has examples of calling DirectX APIs from managed languages.

You can get WindowsAPICodePack-DirectX on Nuget.

jrh
  • 405
  • 2
  • 10
  • 29
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
0

If your application is cross-platform, I would recommend using OpenTK, which is a managed wrapper for OpenGL, and is often easier to learn than DirectX (either managed or native). OpenTK/OpenGL tend to deliver better performance than DirectX as well. With OpenTK, you can draw your image entirely on the GPU, then rasterize it into a bitmap which can interop with the Bitmap class in System.Drawing.

bbosak
  • 5,353
  • 7
  • 42
  • 60