Why not drawing an ellipse with the Pen :: DashPattern set does not produce the expected result?

I am trying to (simply) draw some lines rotating along the path of an ellipse, and thought that I have a nice easy way to do this. Unfortunately, my solution has some problems:

void EllipseDisplayControl::OnPaint(PaintEventArgs^ e) { Graphics^ gfx = e->Graphics; gfx->SmoothingMode = Drawing2D::SmoothingMode::AntiAlias; int width = 100; int height = 10; for( int i = 0; i < 15; i ++ ) { Drawing::Pen^ myPen = (Drawing::Pen^) Drawing::Pens::RoyalBlue->Clone(); //use the standard blue as a start point myPen->Color = Drawing::Color::FromArgb(64, 32, 111, 144); myPen->Width = 3; myPen->DashStyle = Drawing::Drawing2D::DashStyle::Solid; gfx->DrawEllipse(myPen, 0, 50+i*20, width, height); // Draw the blue ring float ellipseCircumference = Math::PI * Math::Sqrt(2* (Math::Pow(0.5*width,2) + Math::Pow(0.5*height,2))); array<Single>^ pattern = {4, ellipseCircumference}; Drawing::Pen^ myPen2 = (Drawing::Pen^) Drawing::Pens::White->Clone(); //use the standard blue as a start point myPen2->DashPattern = pattern; myPen2->DashOffset = i*10; gfx->DrawEllipse(myPen2, 0, 50+i*20, width, height); // Draw the rotating white dot } } 

... produces:

http://www.joncage.co.uk/media/img/BadPattern.png

... so why are the two second ellipses completely white? ... and how can I avoid the problem?

+4
source share
2 answers

This is probably one of the many GDI + errors. This is due to anti-aliasing in combination with DashPattern. It's funny though (well, sort of ...) if you remove SmoothingMode = AntiAlias ​​you will get a wonderful OutOfMemoryException (and if you google on "gdi + pattern outofmemoryexception", you will find hundreds of them. / P>

Since GDI + is not actually supported (although it was also used in the .NET Framework Winforms, BTW I reproduced your problem with .NET C #), as this link can tell us: Pen.DashPattern throw OutOfMemoryException using the default pen , the only way you can probably get around this is to try different values.

For example, if you change the DashOffset parameter instead:

  myPen2->DashOffset = i*ellipseCircumference; 

You will create a good set of ellipses, so perhaps you will find one combination that really suits you. Good luck :-)

+3
source

I cannot imagine that this will fix the problem, but you can extract most of the processing from the loop:

 void EllipseDisplayControl::OnPaint(PaintEventArgs^ e) { Graphics^ gfx = e->Graphics; gfx->SmoothingMode = Drawing2D::SmoothingMode::AntiAlias; int width = 100; int height = 10; Drawing::Pen^ myPen = (Drawing::Pen^) Drawing::Pens::RoyalBlue->Clone(); //use the standard blue as a start point myPen->Color = Drawing::Color::FromArgb(64, 32, 111, 144); myPen->Width = 3; myPen->DashStyle = Drawing::Drawing2D::DashStyle::Solid; float ellipseCircumference = Math::PI * Math::Sqrt(2* (Math::Pow(0.5*width,2) + Math::Pow(0.5*height,2))); array<Single>^ pattern = {4, ellipseCircumference}; Drawing::Pen^ myPen2 = (Drawing::Pen^) Drawing::Pens::White->Clone(); //use the standard blue as a start point myPen2->DashPattern = pattern; for( int i = 0; i < 15; i ++ ) { gfx->DrawEllipse(myPen, 0, 50+i*20, width, height); // Draw the blue ring myPen2->DashOffset = i*10; gfx->DrawEllipse(myPen2, 0, 50+i*20, width, height); // Draw the rotating white dot } } 
+1
source

All Articles