2012-09-13 3 views
2

스트로크 폭에 따라 선형 그라디언트가있는 폴리 라인을 그릴 수 있습니까? 즉, 0과 100 % 및 흰색 50 %의 검정색 그라디언트가있는 경우 검정은 항상 선의 가장자리에 있고 흰색은 각도에 관계없이 가운데에 흰색입니다. 3D 파이프라고 생각하면됩니다. 물론 줄의 획 폭은 최소 10 픽셀입니다. 여기에있는 모든 질문은 끝 사이에 줄을 채우는 방법을 묻습니다. 나는 그것에 관심이 없다. 저는 C#에서 GDI +를 사용하여 일하고 있습니다. 모든 .NET 버전이 될 수 있습니다.GDI +에서 획의 폭을 따라 선형 그라디언트로 폴리 라인을 그립니다.

답변

7

나는 이것이 당신이 원하는 생각 :

Screenshot

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.SmoothingMode=SmoothingMode.AntiAlias; 

     DrawPipe(e.Graphics, 10f, new PointF(10, 10), new PointF(250, 80), Color.White, Color.Black); 

     DrawPipe(e.Graphics, 10f, new PointF(15, 60), new PointF(280, 120), Color.BlueViolet, Color.Black); 
    } 

    private void DrawPipe(Graphics g, float width, PointF p1, PointF p2, Color mid_color, Color edge_color) 
    { 
     SizeF along=new SizeF(p2.X-p1.X, p2.Y-p1.Y); 
     float mag=(float)Math.Sqrt(along.Width*along.Width+along.Height*along.Height); 
     along=new SizeF(along.Width/mag, along.Height/mag); 
     SizeF perp=new SizeF(-along.Height, along.Width); 

     PointF p1L=new PointF(p1.X+width/2*perp.Width, p1.Y+width/2*perp.Height); 
     PointF p1R=new PointF(p1.X-width/2*perp.Width, p1.Y-width/2*perp.Height); 
     PointF p2L=new PointF(p2.X+width/2*perp.Width, p2.Y+width/2*perp.Height); 
     PointF p2R=new PointF(p2.X-width/2*perp.Width, p2.Y-width/2*perp.Height); 

     GraphicsPath gp=new GraphicsPath(); 
     gp.AddLines(new PointF[] { p1L, p2L, p2R, p1R}); 
     gp.CloseFigure(); 

     Region region=new Region(gp); 
     using(LinearGradientBrush brush=new LinearGradientBrush(
      p1L, p1R, Color.Black, Color.Black)) 
     {     
      ColorBlend color_blend=new ColorBlend(); 
      color_blend.Colors=new Color[] { edge_color, mid_color, edge_color }; 
      color_blend.Positions=new float[] { 0f, 0.5f, 1f }; 
      brush.InterpolationColors=color_blend; 
      g.FillRegion(brush, region); 
     } 
    } 
} 

편집 한

대안이 PathGradientBrush

GraphicsPath gp = new GraphicsPath(); 
gp.AddLines(new PointF[] { p1, p1L, p2L, p2, p2R, p1R }); 
gp.CloseFigure(); 

Region region = new Region(gp); 
using (PathGradientBrush brush = new PathGradientBrush(gp)) 
{ 
    brush.CenterColor = mid_color; 
    brush.SurroundColors = new Color[] 
    { 
     mid_color, edge_color,edge_color,mid_color,edge_color,edge_color 
    };   
    g.FillRegion(brush, region); 
} 

을 사용하는 것입니다 먼저 다음 다음 사이에 원을 렌더링함으로써, 여러 라인을 그리는 몇 가지 유물로

using(LinearGradientBrush brush=new LinearGradientBrush(
    p1L, p1R, Color.Black, Color.Black)) 
{ 
    ColorBlend color_blend=new ColorBlend(); 
    color_blend.Colors=new Color[] { 
     Color.FromArgb(0, edge_color), edge_color, mid_color, 
     edge_color, Color.FromArgb(0, edge_color) }; 
    color_blend.Positions=new float[] { 0f, 0.1f, 0.5f, 0.9f, 1f }; 
    brush.InterpolationColors=color_blend; 
    g.FillRegion(brush, region); 
} 

Screenshot2

편집 3 : 편집 2

은 가장자리를 부드럽게 사용하는 몇 가지 알파 투명도를 만들려면 라인들

private void DrawPipes(Graphics g, float width, PointF[] points, Color mid_color, Color edge_color) 
    { 
     for (int i = 0; i < points.Length; i++) 
     { 
      using (GraphicsPath gp = new GraphicsPath()) 
      { 
       gp.AddEllipse(points[i].X - width/2, points[i].Y - width/2, width, width); 

       using (PathGradientBrush brush = new PathGradientBrush(gp)) 
       { 
        brush.CenterColor = mid_color; 
        brush.SurroundColors = new Color[] { edge_color }; 
        brush.CenterPoint = points[i]; 
        g.FillPath(brush, gp); 
       } 
      } 
      if (i > 0) 
      { 
       DrawPipe(g, width, points[i - 1], points[i], mid_color, edge_color); 
      } 
     } 
    } 

Screenshot

+0

이것은 정확히 내가 필요한 것입니다! 나는 그것을보기 위해 아주 피곤하다. 그러나 나는 내일 그것을 할 것이다. 하지만 질문이 남아 있습니다 : 폴리 라인에 적용 할 수 있습니까? 그래디언트가 어떻게해서든지 구부러져 야하는 구석에서 그것을하는 것이 좋지 않을 것이라고 저는 믿습니다. 어쨌든, 이것은 대단한 답변입니다, 정말 고마워요! – Tiborg

+0

폴리 라인으로 위의 새로운 편집을보십시오. – ja72

관련 문제