<p>GDI+ DrawLines function has a clipping bug that can be reproduced by running the following c# code. When running the code, two line paths appear, that should be identical, because both of  them are inside the clipping region. But when the clipping region is set, one of the line segment is not drawn. </p>

<pre><code>protected override void OnPaint(PaintEventArgs e)
{
   PointF[] points = new PointF[] { new PointF(73.36f, 196), 
             new PointF(75.44f, 32), 
             new PointF(77.52f, 32), 
             new PointF(79.6f, 196), 
             new PointF(85.84f, 196) };

   Rectangle b = new Rectangle(70, 32, 20, 164);         
   e.Graphics.SetClip(b);
   e.Graphics.DrawLines(Pens.Red, points); // clipped incorrectly
   e.Graphics.TranslateTransform(80, 0);
   e.Graphics.ResetClip();
   e.Graphics.DrawLines(Pens.Red, points);
 }
</code></pre>

<p>Setting the antials mode on the graphics object resolves this. But that is not a real solution.</p>

<p>Does anybody know of a workaround?</p>

[{'answer_id': 15813, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 0, 'selected': False, 'text': "<p>What appears to be the matter with the code?</p>\n\n<p>OK, the question should be... what should the code do that it doesn't already.</p>\n\n<p>When I run the code, I see 2 red 'spikes' am I not ment to?</p>\n\n<p>You appear to draw the first spike within the clipped rectangle region verified by adding the the following after the declaration of teh Rectangle :</p>\n\n<p><code>e.Graphics.FillRectangle( new SolidBrush( Color.Black ), b );</code></p>\n\n<p>Then you perform a translation, reset the clip so at this point I assume the clientRectangle is being used as the appropriate clip region and then attempt to redarw the translated spike. Where's the bug?!?</p>\n"}, {'answer_id': 15914, 'author': 'Forritarinn', 'author_id': 1873, 'author_profile': 'https://Stackoverflow.com/users/1873', 'pm_score': 0, 'selected': False, 'text': '<p>The bug is that both line segments should be drawn identical but they are not because the spike that is drawn within the clipping region is completely within the clipping region and should not be clipped in any way but it is. This is a very annoying but that results in any software that uses drawlines heavily + clipping to look unprofessional because of gaps that can appear in the polygons.</p>\n'}, {'answer_id': 16914, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 2, 'selected': False, 'text': '<p>It appears that this is a known bug...</p>\n\n<p>The following code appears to function as you requested:</p>\n\n<pre><code>protected override void OnPaint(PaintEventArgs e)\n    {\n        PointF[] points = new PointF[] { new PointF(73.36f, 196), \n         new PointF(75.44f, 32), \n         new PointF(77.52f, 32), \n         new PointF(79.6f, 196), \n         new PointF(85.84f, 196) };\n\n        e.Graphics.SmoothingMode =  System.Drawing.Drawing2D.SmoothingMode.AntiAlias;\n        Rectangle b = new Rectangle(70, 32, 20, 165);\n        e.Graphics.SetClip(b);\n        e.Graphics.DrawLines(Pens.Red, points); // clipped incorrectly\n        e.Graphics.TranslateTransform(80, 0);\n        e.Graphics.ResetClip();           \n        e.Graphics.DrawLines(Pens.Red, points);\n    }\n</code></pre>\n\n<p>Note: I have AntiAlias\'ed the line and extended your clipping region by 1</p>\n\n<p>it appears that the following work arounds might help (although not tested):</p>\n\n<ul>\n<li>The pen is more than one pixel thick</li>\n<li>The line is perfectly horizontal or vertical</li>\n<li>The clipping is against the window boundaries rather than a clip rectangle</li>\n</ul>\n\n<p>The following is a list of articles that might / or then again might not help:</p>\n\n<p><a href="http://www.tech-archive.net/pdf/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0350.pdf" rel="nofollow noreferrer">http://www.tech-archive.net/pdf/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0350.pdf</a>\n<a href="http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0368.html" rel="nofollow noreferrer">http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0368.html</a></p>\n\n<p>OR...</p>\n\n<p>the following is also possible:</p>\n\n<pre><code>protected override void OnPaint ( PaintEventArgs e )\n    {\n        PointF[] points = new PointF[] { new PointF(73.36f, 196), \n         new PointF(75.44f, 32), \n         new PointF(77.52f, 32), \n         new PointF(79.6f, 196), \n         new PointF(85.84f, 196) };\n\n        Rectangle b = new Rectangle( 70, 32, 20, 164 );\n        Region reg = new Region( b );\n        e.Graphics.SetClip( reg, System.Drawing.Drawing2D.CombineMode.Union);\n        e.Graphics.DrawLines( Pens.Red, points ); // clipped incorrectly\n        e.Graphics.TranslateTransform( 80, 0 );\n        e.Graphics.ResetClip();\n        e.Graphics.DrawLines( Pens.Red, points );\n    }\n</code></pre>\n\n<p>This effecivly clips using a region combined/unioned (I think) with the ClientRectangle of the canvas/Control. As the region is difned from the rectangle, the results should be what is expected. This code can be proven to work by adding</p>\n\n<pre><code>e.Graphics.FillRectangle( new SolidBrush( Color.Black ), b );\n</code></pre>\n\n<p>after the setClip() call. This clearly shows the black rectangle only appearing in the clipped region.</p>\n\n<p>This could be a valid workaround if Anti-Aliasing the line is not an option.</p>\n\n<p>Hope this helps</p>\n'}]
['https://Stackoverflow.com/questions/15478', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1873/']