GDI+ P/Invoke
-
Hello. I have searched for a way to P/Invoke the Graphics.FillPolygon(Brush, Point[]) method. I saw that it is much faster to do so (likely because it bypasses error checking that is unneeded in a well-made program). However, I have not found any examples of how to do so (even .NET Reflector is of no help, only referring me to a P/Invoke that uses a private native field). Please help me translate this code into a P/Invoke. The FillPolygon method is the most important by far, as it is called 10 times more often in the real code as the Clear method is. This Render method takes up an average of 75% of the time in the code, as profiled by NProf (a specially modified version that samples 5x as often as usual was used).
public static void Render(Color color, SolidBrush brush, Point[] points, Graphics graphics)
{
graphics.Clear(color);
graphics.FillPolygon(brush, points);
} -
Hello. I have searched for a way to P/Invoke the Graphics.FillPolygon(Brush, Point[]) method. I saw that it is much faster to do so (likely because it bypasses error checking that is unneeded in a well-made program). However, I have not found any examples of how to do so (even .NET Reflector is of no help, only referring me to a P/Invoke that uses a private native field). Please help me translate this code into a P/Invoke. The FillPolygon method is the most important by far, as it is called 10 times more often in the real code as the Clear method is. This Render method takes up an average of 75% of the time in the code, as profiled by NProf (a specially modified version that samples 5x as often as usual was used).
public static void Render(Color color, SolidBrush brush, Point[] points, Graphics graphics)
{
graphics.Clear(color);
graphics.FillPolygon(brush, points);
}This is the MSDN page[^] for the Polygon function in gdi32. You need to set the brush and fill mode first and it will draw it filled for you. [Added]This page[^] gives you the gdi+ function signatures[/Added]
Dave
Tip: Passing values between objects using events (C#)
_BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Saturday, March 6, 2010 4:27 PM
_
-
This is the MSDN page[^] for the Polygon function in gdi32. You need to set the brush and fill mode first and it will draw it filled for you. [Added]This page[^] gives you the gdi+ function signatures[/Added]
Dave
Tip: Passing values between objects using events (C#)
_BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)modified on Saturday, March 6, 2010 4:27 PM
_
Thanks, but how do I apply this using C# code? Oh, and what you showed me is normal GDI. System.Graphics uses GDI+. Is this any problem? (I have heard normal GDI is faster, though; is this true?). Here is a slightly modified version of the context within which the code is used:
if (!isPerformanceTest) { Renderer.Render(newDrawing /\*The real method calls for this; this is an object that contains the Point\[\], Color, and SolidBrush\*/, g)); } bitmapData = bitmap.LockBits( new Rectangle(0, 0, (int)(Tools.MaxWidth / reduction), (int)(Tools.MaxHeight / reduction)), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// After this, some highly optimized math is done to compare each pixel of the drawing to the input picture
I would like some code if at all possible.
-
Thanks, but how do I apply this using C# code? Oh, and what you showed me is normal GDI. System.Graphics uses GDI+. Is this any problem? (I have heard normal GDI is faster, though; is this true?). Here is a slightly modified version of the context within which the code is used:
if (!isPerformanceTest) { Renderer.Render(newDrawing /\*The real method calls for this; this is an object that contains the Point\[\], Color, and SolidBrush\*/, g)); } bitmapData = bitmap.LockBits( new Rectangle(0, 0, (int)(Tools.MaxWidth / reduction), (int)(Tools.MaxHeight / reduction)), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// After this, some highly optimized math is done to compare each pixel of the drawing to the input picture
I would like some code if at all possible.
I'd have to work at and test a C# PInvoke method. I have edited the original post with a link to the MSDN GDI+ page[^] with the GdipFillPolygon function signature (gdipluss.dll).
Dave
Tip: Passing values between objects using events (C#)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I'd have to work at and test a C# PInvoke method. I have edited the original post with a link to the MSDN GDI+ page[^] with the GdipFillPolygon function signature (gdipluss.dll).
Dave
Tip: Passing values between objects using events (C#)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)Here is the complete source code of the program. It may only be used under the provisions of the GPL. This is not Roger Alsing's original work; it has been very highly optimized. Further optimization of other sections is almost useless without optimization of the renderer. I tried Direct3D, which was very good, but it had WAY too many bugs to even attempt to continue on. The Render method is in the Renderer.cs file and is called by FitnessCalculator.cs, which is in turn called by MainForm.cs. The DnaPolygon.cs and DnaDrawing.cs files are used extensively. Download Source Code The two lines of the Render method that I mentioned in the original post together take more than 70% of the CPU time of the program.