【问题标题】:Draw overlapping lines with same transparency绘制具有相同透明度的重叠线
【发布时间】:2019-01-02 12:22:57
【问题描述】:

我大致有这样的逻辑:

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

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

如果我画这条线,重叠部分的颜色与其余部分不同,这是由于相同部分的混合(首先,1 与背景混合,然后 2 与已经混合 1 与背景)。有没有办法,如何达到效果,重叠部分与单个非重叠段具有相同的颜色?

【问题讨论】:

    标签: c# graphics gdi+


    【解决方案1】:

    DrawLines 在这种情况下不起作用,因为它只会在 one go 中绘制 connected 行。

    您需要使用StartFigure 将行集添加到一个 GraphicsPath分隔这两个集。

    例如,Drawline 在左侧,DrawPath 在右侧:

    这是两者的代码:

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

    不要忘记PenGraphics 对象的Dispose,或者,最好将它们放在using 子句中!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多