I have a control that contains shapes drawn with GDI+ and the user can zoom into it. If he zooms in far the actual coordinates that the shapes are drawn at get quite large.
Now I zoomed into the location of the left edge of a circle drawn by FillEllipse
and after a while the circle suddenly flips and I actually see a right edge instead of a left edge.
I extracted the coordinates and put up a simple program that reproduces the problem (All thats needed for it is a form, with two radiobuttons and a picturebox of sufficient size on it):
Public Class Form1
Private Sub Draw()
'Create a canvas
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Using g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.Black)
'X location, Y location and the diameter of the circle to draw
Dim x, y, dia As Single
If RadioButton1.Checked Then
'This works as expected
x = 561
y = -3993174.7
dia = 7986931.0
Else
'This comes out flipped
x = 561
y = -4436903.7
dia = 8874368.0
End If
'Define and draw the ellipse
Dim rectf As New RectangleF(x, y, dia, dia)
g.FillEllipse(Brushes.Red, rectf)
g.DrawEllipse(Pens.LightGreen, rectf)
'Mark the circle's vertical center
g.DrawLine(Pens.Blue, 0.0F, y + dia / 2, CSng(bmp.Width), y + dia / 2)
End Using
'Display the image
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
PictureBox1.Image = bmp
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
'Redraw on radiobutton check change
Draw()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Draw()
End Sub
End Class
In the images below the red part is the ellipse that is being drawn, black is the background, and the blue line is the vertical center of the ellipse as calculated from the coordinates I put in.
If you run that program and select RadioButton1
you will see the following:
All is well, the circle is drawn to the right.
However if you select RadioButton2
you see:
The circle suddenly is flipped horizontally and is drawn to the left.
I suspect that somehow the large values interfere but I honestly can't see a reason how this outcome can be produced. Can someone shed some light why GDI+ gets upset and how I can fix it?
Both answers in VB.NET and in C# are very much appreciated.