Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3420598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:01:50+00:00 2026-05-18T06:01:50+00:00

Any suggestions on how to draw a directed line in .Net? I’m currently using

  • 0

Any suggestions on how to draw a directed line in .Net? I’m currently using Graphics.DrawLine with a Pen with dashes. But the dashes point in both directions. I want them to all point in a single direction.

using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
{
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Dash;
    p.DashCap = DashCap.Triangle;
    e.Graphics.DrawLine(p, startPt, pt);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:01:50+00:00Added an answer on May 18, 2026 at 6:01 am

    This might not be very helpful, but the way that we accomplished this type of thing on other projects that I have been involved with is to implement a custom “pen” that accepts the line to be drawn, calculates the actual dashes, and then draws each dash as an individual line with a “real” Pen. If you were to do something like this, then you could actually apply a “pen” level StartCap and/or EndCap to each dash.

    The custom “pen” would have to accept an array or sequence of dash/gap lengths (or maybe an enumeration of standard pen styles that would be interpreted internally as specific dash/gap lengths).

    We did this many years ago with a GDI-based drawing system where we had to support more line styles than the built in line styles that GDI supported.

    It is an interesting exercise, but it is also probably more trouble than it is worth unless you REALLY have to have the dashes drawn as you describe.

    [EDIT]

    Here is some sample code for how you might do this, if you are so inclined.

    First is an extension method on the Graphics object. It takes a pen, dash length, gap length, start point, and end point. It interpolates along the line p1->p2, drawing a “dash” length segment and then skipping a “gap” length segment. The passed-in pen should be solid with an arrow endcap (to achieve the effect you were asking for).

    The code is pretty rough, but the behavior is, more or less, like this:
    If the input line is shorter then the total dash+gap length, then the line is drawn as-is using the input pen.
    If the input line is longer than the total dash+gap length, then “dash” length lines are drawn until the “remainder” of the line is less than the dash+gap length, at which point the remainder is drawn as-is with the input pen.

    If you wanted to implement Path drawing, then you would have to interpolate around the intermediate vertices (unless you wanted to cheap out and just compute dashes for each segment indepdently).

        public static void DrawDashedLine(this Graphics g, Pen p, float dash, float gap, PointF s, PointF e)
        {
          float dx = e.X - s.X;
          float dy = e.Y - s.Y;
    
          float len = (float)Math.Sqrt(dx * dx + dy * dy);
          float remainder = len;
    
          float vx = dx / len;
          float vy = dy / len;
    
          if (len <= dash + gap)
          {
            g.DrawLine(p, s, e);
            return;
          }
    
          PointF last = s;
    
          while (remainder > dash + gap)
          {
            PointF p1 = new PointF(last.X, last.Y);
            PointF p2 = new PointF(p1.X + vx*dash, p1.Y + vy*dash);
    
            g.DrawLine(p, p1, p2);
    
            last = new PointF(p2.X + vx*gap, p2.X + vy*gap);
    
            remainder = remainder - dash - gap;
          }
    
          if (remainder > 0)
          {
            g.DrawLine(p, last, e);
          }
        }
      }
    

    Here is how you would call it:

    private void button1_Click(object sender, EventArgs e)
    {
      using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
      {
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.ArrowAnchor;
        p.CustomEndCap = new AdjustableArrowCap(3, 3);
        p.DashStyle = DashStyle.Solid;
        var graph = this.CreateGraphics();
        graph.DrawDashedLine(p, 20, 10, new PointF(20, 20), new PointF(500, 500));
      } 
    }
    

    I don’t claim that the code is great (or even necessarily good ;-), but it should give you a good starting point if you are really interested in drawing your directed line.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any suggestions how to create a line by clicking two new points then draw
Any suggestions? Using visual studio in C#. Are there any specific tools to use
Any suggestions for good open source asp.net (C#) apps out there which meet as
I can't find any way to draw a force directed graph where the weighting
Any suggestions on the best way to ensure thread safety when changing the properties
Any suggestions on how to write repeatable unit tests for code that may be
Any suggestions on how to improve DataGridViewComboBoxColumn performace with large item sets? I've got
Any suggestions for an accurate Web Log analysis tool to generate reports on the
Any suggestions on why a VB6 program would be slower when compiled than when
any suggestions for a C# object pooling framework? requirements are multi-thread support and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.