绘制具有相同透明度的重叠线
Draw overlapping lines with same transparency
我大概有这样的逻辑:
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 与背景混合)。有没有办法,如何实现重叠部分与单个非重叠部分具有相同颜色的效果?
DrawLines
在这种情况下将不起作用,因为它只会在 one 中绘制 connected 行。
您需要将行集添加到一个 GraphicsPath
using StartFigure
to separate 这两个集.
示例,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);
}
不要忘记 Pen
和 Graphics
对象的 Dispose
,或者最好将它们放在 using
子句中!
我大概有这样的逻辑:
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 与背景混合)。有没有办法,如何实现重叠部分与单个非重叠部分具有相同颜色的效果?
DrawLines
在这种情况下将不起作用,因为它只会在 one 中绘制 connected 行。
您需要将行集添加到一个 GraphicsPath
using StartFigure
to separate 这两个集.
示例,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);
}
不要忘记 Pen
和 Graphics
对象的 Dispose
,或者最好将它们放在 using
子句中!