Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GDI+ P/Invoke

GDI+ P/Invoke

Scheduled Pinned Locked Moved C#
graphicshelpcsharpc++winforms
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StarBP
    wrote on last edited by
    #1

    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);
    }

    D 1 Reply Last reply
    0
    • S StarBP

      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);
      }

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      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

      _

      S 1 Reply Last reply
      0
      • D DaveyM69

        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

        _

        S Offline
        S Offline
        StarBP
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • S StarBP

          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.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          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)

          S 1 Reply Last reply
          0
          • D DaveyM69

            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)

            S Offline
            S Offline
            StarBP
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups