绘制透明度相同重叠线

问题描述 投票:2回答:1

我大致有这样的逻辑:

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

的问题是,点1和points2包含被重叠一些线段。

如果我画这个线条,重叠的部分具有不同的颜色,然后其余部分,由于相同的段的混合(第一,1与背景和后2与已经混合1与背景)。有没有一种方法,如何达到的效果,即重叠部分有颜色作为单一的非重叠segmens一样吗?

c# graphics gdi+
1个回答
5
投票

DrawLines不会在这种情况下工作,因为它只会在一气呵成画连接线。

你需要使用GraphicsPath到两套独立的行集添加到一个StartFigure

例如,Drawline向左,DrawPath向右:

enter image description here

下面是两个代码:

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

不要忘了DisposePenGraphics对象,或者更好的,把他们在using条款!

© www.soinside.com 2019 - 2024. All rights reserved.