简体   繁体   English

.NET 使用 GDI+ 绘制带轮廓线

[英].NET draw line with outline using GDI+

I'm using .NET for a desktop application.我将 .NET 用于桌面应用程序。

I'm drawing some lines with cap start and end to use them as arrows, using Graphics.DrawLine with an apropiate pen for it, with some width (lets say 8px).我正在绘制一些带有大写开始和结束的线条以将它们用作箭头,使用 Graphics.DrawLine 和合适的笔,具有一些宽度(假设为 8px)。

Now what i want to achive is to add an "outline" to this lines in other color, with 1 or 2 pixels width.现在我想要实现的是在其他颜色的线条中添加一个“轮廓”,宽度为 1 或 2 个像素。

I don't find options to do this using GDI+ (if there is an option for this, perhaps it is in "gdiplus.dll" and i could import it with dllimport).我没有找到使用 GDI+ 执行此操作的选项(如果有此选项,也许它在“gdiplus.dll”中,我可以使用 dllimport 导入它)。

My first attempt was to draw first the same line with a wider pen, but the effect doesn't fit to the arrow ends as you can imagine.我的第一次尝试是先用更宽的笔绘制同一条线,但效果并不像您想象的那样适合箭头末端。

Any ideas with this?有什么想法吗?

UPDATE: This is the image with my first attempt drawing two lines, one over the other:更新:这是我第一次尝试绘制两条线时的图像,一条在另一条上:

使用不同笔宽的两次 LineDraw 操作执行的轮廓线

A little late for this answer but I have one that works better, not perfect but better这个答案有点晚了,但我有一个更好的,不完美但更好

draw the arrow 4 times with different off sets then draw the main one over it like this用不同的偏移量绘制箭头 4 次,然后像这样在它上面绘制主要的箭头

Using bigArrow As New Drawing2D.AdjustableArrowCap(5, 5)
   ' Shadow
   Using objPen As New Pen(Color.Black, 5)
      objPen.CustomEndCap = bigArrow
      gr.DrawLine(objPen, 50 - 2, 50 - 2, 100 - 2, 100 - 2)
      gr.DrawLine(objPen, 50 + 2, 50 - 2, 100 + 2, 100 - 2)
      gr.DrawLine(objPen, 50 + 2, 50 + 2, 100 + 2, 100 + 2)
      gr.DrawLine(objPen, 50 - 2, 50 + 2, 100 - 2, 100 + 2)
   End Using
   
   ' Line
   Using objPen As New Pen(Color.CornflowerBlue, 5)
      objPen.CustomEndCap = bigArrow
      gr.DrawLine(objPen, 50, 50, 100, 100)
   End using
End Using

it misses the corners but isn't too bad它错过了角落,但还不错

在此处输入图片说明

after much experimenting I finally got it经过多次试验,我终于明白了

use a GraphicsPath and the widen command, the one other trick is widen also widens the line to the point it sticks out past the End cap arrow, so use WidthScale on the end cap as well, then just reduce the size of the end cap使用 GraphicsPath 和加宽命令,另一个技巧是加宽也将线加宽到它伸出端盖箭头的点,所以也在端盖上使用 WidthScale,然后只减小端盖的大小

Using bigArrow As New Drawing2D.AdjustableArrowCap(3, 3)
   bigArrow.WidthScale = 1.5
   
   Using objPen As New Pen(Color.Black, 10)
      objPen.CustomEndCap = bigArrow

      Using objPath As New Drawing2D.GraphicsPath
         objPath.AddLine(50, 50, 100, 100)
         Using objWidenPen As New Pen(Color.Empty, 10)
            objWidenPen.CustomEndCap = bigArrow
            objPath.Widen(objWidenPen)
         End Using

         gr.DrawPath(objPen, objPath)

      End Using
   End Using

   Using objPen As New Pen(Color.CornflowerBlue, 10)
      objPen.CustomEndCap = bigArrow
      gr.DrawLine(objPen, 50, 50, 100, 100)
   End Using
End Using

the result looks much better结果看起来好多了

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM