There seems to be a million questions out there on this, yet I can't find one that will work. So, I guess it's time for question 1,000,001.
I have a custom control with a PictureBox
and a Panel
. The Panel
is the child of PictureBox
with a transparent background. This allows me tp draw on top of whatever image is loaded in the PictureBox
.
The drawing part works, but the erasing part does not. And if I use Invalidate()
I just get a bunch of flickering, and the line never even shows.
If the end goal isn't obvious, it should work like any decent drawing application, where you click in one spot, drag around, and the line moves with the mouse until you let go.
Code:
private void drawLine(Point pt) {
// Erase the last line
if (m_lastPoints != null) {
m_graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
m_graphics.DrawLine(m_transPen, m_lastPoints[0], m_lastPoints[1]);
}
// Set the last points
m_lastPoints = new Point[] { m_mouseStartPoint, pt };
m_graphics.DrawLine(new Pen(m_color), m_mouseStartPoint, pt);
}
m_transPen
is defined as new Pen(Color.FromArgb(0, 0, 0, 0));
And the result:
Now, if I change it to:
m_graphics.DrawLine(Pens.White, m_lastPoints[0], m_lastPoints[1]);
I get this, which shows what it should be doing, only instead of with white lines, they should be transparent.