3
votes

I have roughly this logic:

Bitmap bmp = ....
Pen pen = new Pen(Color.FromArgb(125, 0, 0, 255), 15);
var graphics = Graphics.FromImage(bmp);
graphics.DrawLines(pen, points1);
graphics.DrawLines(pen, points2);

The problem is, that points1 and points2 contain some line segments that are overlaping.

If I draw this lines, the overlapping part have a different color then the rest, due to the blending of the same segments (first, 1 with background and later 2 with already blended 1 with background). Is there a way, how to achieve the effect, that the overlapping parts have the same color as a single non-overlapping segmens?

1

1 Answers

5
votes

DrawLines will not work in this case as it will only draw connected lines in one go.

You need to add the line sets to one GraphicsPath using StartFigure to separate the two sets.

Example, Drawline to the left, DrawPath to the right:

enter image description here

Here is the code for both:

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
..
Pen pen = new Pen(Color.FromArgb(125, 0, 0, 255), 15)
   { LineJoin = LineJoin.Round };
var graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
graphics.DrawLines(pen, points1);
graphics.DrawLines(pen, points2);
bmp.Save("D:\\__x19DL", ImageFormat.Png);

graphics.Clear(Color.White);
using (GraphicsPath gp = new GraphicsPath())
{
    gp.AddLines(points1);
    gp.StartFigure();
    gp.AddLines(points2);
    graphics.DrawPath(pen, gp);
    bmp.Save("D:\\__x19GP", ImageFormat.Png);
}

Don't forget to Dispose of the Pen and the Graphics object, or, better, put them in using clauses!