The reason of the misbehavior is in the line
System.Drawing.Pen bPen = aPen;
This code does't create a new Pen
instance, instead it declares a new reference to the existing one which is aPen, Since then there's one instance of Pen class and two references to it: aPen
and bPen
.
P.S. Pen is IDisposable
so do not forget to dispose it in order to prevent resource leak (hPen in your case):
using (System.Drawing.Pen aPen = new Pen(Color.Firebrick, 5)) {
aPen.DashStyle = DashStyle.Solid;
// Paint with aPen here
...
}
using (System.Drawing.Pen bPen = new Pen(Color.Firebrick, 1)) {
bPen.DashStyle = DashStyle.Dot;
// Paint with bPen here
...
}