This "DrawLines" method is somewhat unsuccessful (and several other methods in .NET-GDI + -API). First of all, it only accepts an array, which means that the number of points must exactly match the size of the array; if you do not know how many points you will have in preparing the points, you need to call Array.Resize, which will copy the data. Secondly, the first action in the DrawLine method is copying points - why not transfer the original data to GDI +? In fact, data is copied twice before it gets into GDI +.
What can be done about this:
- Use the GDI + Flat API from C # (using P / Invoke).
- Use the (native) C ++ API] (for example, use the C ++ / CLI to interact with C #).
The first idea might look something like this:
public void DrawLines(System.Drawing.Color color, ref System.Drawing.Point[] points, int pointsCount) { IntPtr pen = IntPtr.Zero; int status = GdipCreatePen1( color.ToArgb(), 1, (int)GraphicsUnit.World, out pen); unsafe { fixed (Point* pointerPoints = points) { status = GdipDrawLinesI(new HandleRef(this, this.handleGraphics), new HandleRef(this, pen), (IntPtr)pointerPoints, pointsCount); } } status = GdipDeletePen(new HandleRef(this, pen)); } [DllImport(GDIPlusDll, SetLastError = true, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)] private static extern int GdipDrawLinesI(HandleRef graphics, HandleRef pen, IntPtr points, int count); [DllImport(GDIPlusDll, SetLastError = true, ExactSpelling = true)] private static extern int GdipDeletePen(HandleRef pen); [DllImport(GDIPlusDll, SetLastError = true, ExactSpelling = true)] private static extern int GdipCreatePen1(int argb, float width, int unit, out IntPtr pen);
BTW - you can access your own handles in .NET-GDI + objects using reflection (of course, this is undocumented, you need to access a private method). For a System.Drawing.Font object, it looks something like this:
Type type = typeof(System.Drawing.Font); System.Reflection.PropertyInfo propInfoNativeFontHandle = type.GetProperty("NativeFont", System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); System.Drawing.Font font = ... IntPtr nativeHandle = propInfoNativeFontHandle.GetValue(font, null)