1

I'm using DashStyle.Dash while rendering my hierarchy of objects. My application uses Graphics.Transform extensively and I find that at some scale values (including a scale of 100%) and some angles of rotation, Graphics.DrawLine throws OutOfMemoryException when using a pen with DashStyle.Dash. Using Google I found that this is a well-known problem. Microsoft tells us that this is not a blocking issue. So far, I have not found a workaround for this problem.

What are my options?

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • 1
    I assume you're Disposing of the Pen? And one of the articles mentioned said to avoid duplicate points in Graphics.DrawLine*s*. You are using the singular version, correct? You might post some code; maybe that will help get some answers. – Paul Williams Aug 14 '09 at 19:43
  • You definitely have to Dispose all your GDI+ objects. I have had many problems when I didn't, and one of them was an Out of Memory exception. – Sebastian Aug 24 '09 at 20:12
  • Another idea is to take a profiler and see what's may be going on with the code, if it's a memory leak for example, and help you to solve it. – Sebastian Aug 24 '09 at 20:13
  • I am of the opinion that the memory in question here is that of the graphics card. And I think the problem is that I was caching pens between WM_PAINT events which some have mentioned as a bad idea. – Agnel Kurian Aug 25 '09 at 07:27

1 Answers1

1

I just encountered the same issue. Upon further investigation I discovered the following.

  1. The line length was exactly the custom dash pattern length. Changing the line length +/- 1 pixel avoided the error.
  2. I was using LineCapRound and DashCapRound, switching to LineCapSquare and DashCapFlat avoided the error
  3. The error came from trying flatten the path (presumably from the rounded ends)
  4. The pen width was zero.

My final solution was to not draw zero width lines!

Stephen Nutt
  • 3,258
  • 1
  • 21
  • 21