title: c# Drawing with gdi32.dll vs System.Drawing.Graphics
body: <p>So I have some code that creates a highlight effect on top of a picture box with gdi32.dll and I am wondering if there is a simpler way to do it with <code>System.Drawing.Graphics</code>? Basically with the gdi32.dll, I have to capture a screen shot after drawing, post that to my picturebox and then I am able to draw more stuff and change the color of the pen that I use. If I just try to change the pen thickness and color and draw on the screen again, if changes what I already drew.</p>
  
  <p>Now I have a version of this that uses <code>System.Drawing.Graphics</code> and lots of math with <code>FillPolygon</code> but if I draw over an area I already drew on, it just makes the area I drew on darker. It does not do this with gdi32.dll which justr shades as long as you have not already shaded the area with the mouse. Any suggestions?</p>
  
  <pre><code>public partial class Form9 : Form
  {
      private bool is_mouse_down { get; set; } // Will check if the mouse is down or not.
  
      private Color Pen_Color = new Color();
  
      private int Pen_Type { get; set; }
  
      private int Thickness { get; set; }
  
      private bool Start { get; set; }
  
      List&lt;Point&gt; Points = new List&lt;Point&gt;();
  
      public Form9()
      {
          InitializeComponent();
  
          pictureBox1.Dock = DockStyle.Fill;
  
          Pen_Color = Color.Blue;
          Pen_Type = 13; // Type = 9 for highlighter, Type = 13 for solid.
          Thickness = 2;
          Start = false;
  
          pictureBox1.MouseDown += pictureBox1_MouseDown;
          pictureBox1.MouseUp += pictureBox1_MouseUp;
          pictureBox1.MouseMove += pictureBox1_MouseMove;
          pictureBox1.Paint += pictureBox1_OnPaint;
      }
  
      private void DrawHighlight(Graphics g, Point[] usePoints, int brushSize, int penType, Color brushColor)
      {
          int useColor = System.Drawing.ColorTranslator.ToWin32(brushColor);
          IntPtr pen = GetImage.GDI32.CreatePen(GetImage.GDI32.PS_SOLID, brushSize, (uint)useColor);
          IntPtr hDC = g.GetHdc();
          IntPtr xDC = GetImage.GDI32.SelectObject(hDC, pen);
          GetImage.GDI32.SetROP2(hDC, penType);//GetImage.GDI32.R2_MASKPEN);
          for (int i = 1; i &lt;= usePoints.Length - 1; i++)
          {
              Point p1 = usePoints[i - 1];
              Point p2 = usePoints[i];
              GetImage.GDI32.MoveToEx(hDC, p1.X, p1.Y, IntPtr.Zero);
              GetImage.GDI32.LineTo(hDC, p2.X, p2.Y);
          }
          GetImage.GDI32.SetROP2(hDC, GetImage.GDI32.R2_COPYPEN);
          GetImage.GDI32.SelectObject(hDC, xDC);
          GetImage.GDI32.DeleteObject(pen);
          g.ReleaseHdc(hDC);
      }
  
      private void pictureBox1_OnPaint(object sender, PaintEventArgs e)
      {
          if (Start)
          {
              base.OnPaint(e);
              if (is_mouse_down)
              {
                  DrawHighlight(e.Graphics, Points.ToArray(), Thickness, Pen_Type, Pen_Color);
              }
          }
      }
  
      private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
      {
          Points.Clear();
  
          Start = true;
          is_mouse_down = true;
      }
  
      private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
      {
          is_mouse_down = false;
  
          using (Image img = CaptureScreen())
          {
              try
              {
                  if (System.IO.File.Exists(Program.ProgramPath + @"\Temp\marked.bmp"))
                  {
                      System.IO.File.Delete(Program.ProgramPath + @"\Temp\marked.bmp");
                  }
              }
              catch (Exception Ex)
              {
                  MessageBox.Show("File Delete Error" + Environment.NewLine + Convert.ToString(Ex));
              }
  
              try
              {
                  img.Save(Program.ProgramPath + @"\Temp\marked.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
              }
              catch (Exception Ex)
              {
                  MessageBox.Show("Unable to save Screenshot" + Environment.NewLine + Convert.ToString(Ex));
              }
          }
  
          if (System.IO.File.Exists(Program.ProgramPath + @"\Temp\marked.bmp"))
          {
              using (FileStream fs = new System.IO.FileStream(Program.ProgramPath + @"\Temp\marked.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
              {
                  pictureBox1.Image = Image.FromStream(fs);
              }
          }
  
          pictureBox1.Invalidate(); // Refreshes picturebox image.
      }
  
      public Image CaptureScreen()
      {
          GetImage gi = new GetImage();
  
          return gi.CaptureWindow(GetImage.User32.GetDesktopWindow());
      }
  
      private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
      {
          if (is_mouse_down == true) // Check to see if the mouse button is down while moving over the form.
          {
              Points.Add(new Point(e.X, e.Y));
  
              pictureBox1.Invalidate(); // Refreshes picturebox image.
          }
      }
  </code></pre>
  
  <p>Here are a couple photos of what I am talking about:</p>
  
  <p>Using <code>System.Drawing.Graphics</code>:
  <a href="https://i.stack.imgur.com/4Ddho.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Ddho.png" alt="Using &lt;code&gt;System.Drawing.Graphics&lt;/code&gt;"></a></p>
  
  <p>Using <code>gdi32.dll</code>:
  <a href="https://i.stack.imgur.com/kC0Y1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kC0Y1.png" alt="Using &lt;code&gt;gdi32.dll&lt;/code&gt;"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After testing some of your code...I got a some strange stuff.</p>
  
  <p><a href="https://i.stack.imgur.com/3Wisy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Wisy.png" alt="enter image description here"></a></p>