1

I want to draw an arrow using C#

I can write the following:

protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);
   Pen p = new Pen(Color.Blue, 1);
   p.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
   p.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
   e.Graphics.DrawLine(p, 20, 20, 100, 100);
}

But the cap is going to be very small. Is there away to get around this problem (without changing Pen width)?

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
  • 1
    Draw a line without a cap, then draw another very thick line, very short at the end with the cap. – Ron Beyer Jul 09 '15 at 15:35
  • 1
    You need to free graphics resources you create when you're done with them. – xxbbcc Jul 09 '15 at 15:39
  • @xxbbcc: Probably even create them outside the paint function and dispose them with the class, depends how frequent it's repainting and how much it has to paint (might be more code we don't see)... or even just a single static resource shouldn't be a problem – musefan Jul 09 '15 at 15:40
  • @musefan, I would not do that, GDI+ resources are limited (10,000 max) and reserving them when not in use can cause issues in other places that become very hard to track down. – Ron Beyer Jul 09 '15 at 15:42
  • 1
    @RonBeyer: I have seen good performance improvements by not initializing inside of paint method. And to be realistic, when would it not be in use? We are talking a paint event, it can redraw at anytime. Surely better to reserve the Pen for painting rather than the paining fail because it can't create a new Pen... perhaps you just mean my suggestion of static resource then maybe it's not the best idea, but again maybe every form has paint events that use the same blue pen... then it probably would be beneficial. Not saying either of us is right, just that I think it depends on the situation. – musefan Jul 09 '15 at 15:47
  • Only cache GDI+ resources for a single rendering pass. They're a limited resource. – xxbbcc Jul 09 '15 at 16:12

0 Answers0