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 min开发者_运维问答e is client compatibility especially remote desktop. AFAIK remote desktop does not support OGL/DirectX out of the box.[/EDIT]
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:
TextRenderer.MeasureText
TextRenderer.DrawText
.
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
- MSDN: Using a Cached Bitmap to Improve Performance
- MSDN: CachedBitmap Class
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.
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.
The Microsoft API Code Pack (Archived Feb. 2010) has examples of calling DirectX APIs from managed languages.
You can get WindowsAPICodePack-DirectX on Nuget.
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.
精彩评论