我想從c#GDI + DrawLines函數中獲得更多的性能。當我在代碼上運行一個分析器時,我發現幾乎有一半的時間花在DrawLines函數中,準備將點數組發送到本地GDI + dll。這似乎是一個很大的開銷,我想知道是否有人可以想出一個更有效的方式與DrawLines函數進行交互,而不是使用System.Drawing中的DrawLines函數的本地實現。可能的GDI + DrawLines優化思考
以下是本機Systemm.Drawing功能:
public void DrawLines(Pen pen, PointF[] points)
{
if (pen == null)
{
throw new ArgumentNullException("pen");
}
if (points == null)
{
throw new ArgumentNullException("points");
}
IntPtr handle = SafeNativeMethods.Gdip.ConvertPointToMemory(points);
int status = SafeNativeMethods.Gdip.GdipDrawLines(new HandleRef(this,this.NativeGraphics), new HandleRef(pen, pen.NativePen), new HandleRef(this, handle), points.Length);
Marshal.FreeHGlobal(handle);
this.CheckErrorStatus(status);
}
internal static IntPtr ConvertPointToMemory(PointF[] points)
{
if (points == null)
{
throw new ArgumentNullException("points");
}
int num2 = Marshal.SizeOf(new GPPOINTF().GetType());
int length = points.Length;
IntPtr ptr = Marshal.AllocHGlobal((int) (length * num2));
for (int i = 0; i < length; i++)
{
Marshal.StructureToPtr(new GPPOINTF(points[i]), (IntPtr) (((long) ptr) + (i * num2)), false);
}
return ptr;
}
謝謝。 Direct2D是未來的道路,但是現在我被困在GDI +中,這並不是那麼糟糕。如果c#wrapper沒有這樣的開銷,那麼可能會更好,因此我的問題。 – Einar 2010-11-07 01:25:15
稍微偏離主題,但值得指出的是,您可以使用Window API代碼包訪問Direct2D界面。 – Samuel 2010-11-07 01:32:45