I’m experiencing an issue with GDI+ while custom painting dashed rectangles.
The vertical portion of dashed rectangles appear as solid, continuous lines when the window size is increased or when scrolling up/down. Moving the mouse faster results in fewer and fewer solid sections. Curiously the horizontal lines do not exhibit this behaviour and appear as expected.
So far two non-optimal solutions have been to set ResizeRedraw = true or to call Invalidate() during OnResize() and OnScroll(). I’d of course like to avoid this as what I am really drawing is more complex and these slow calls destroy the fluid experience. I’ve also tried invalidating only the newly shown area to no avail – only a full Invalidate seems to work.
Any pointers on how to work this out?
Demo code:
using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public class Form1 : Form { static void Main() { Application.Run(new Form1()); } public Form1() { this.ClientSize = new System.Drawing.Size(472, 349); DoubleBuffered = true; //ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); int dimensions = 70; using ( Pen pen = new Pen(Color.Gray) ) { pen.DashStyle = DashStyle.Dash; for ( int x = 0; x < 20; ++x ) { for ( int y = 0; y < 20; ++y ) { Rectangle rect = new Rectangle(x * dimensions, y * dimensions, dimensions, dimensions); e.Graphics.DrawRectangle(pen, rect); } } } } }
I think there are two problems: there appears to be an area at the edge of the window where rectangles aren’t drawn correctly; and you’re drawing the rectangles over each other, so the dashing won’t work properly.
Replace your OnPaint loop with the following: