GDI+, large translation values, and distorted rendering of graphic primitives

StackOverflow https://stackoverflow.com/questions/23145869

  •  05-07-2023
  •  | 
  •  

Pregunta

Long-time user of S.O., but first-time poster. Please be gentle. :)

Using .NET's GDI+ (any version of the Framework, from 2,0 on up to 4.5.1), I get very strange behaviour when rendering graphics primitives with large translation values on the 'Graphics' object. Here is a very simple piece of code that demonstrates the issue...simply paste in into a windows form and run:

 Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    e.Graphics.TranslateTransform(183946953, 82803840)

    Dim r As Rectangle = New Rectangle(New Point(-183946600.0, -82803570.0), New Size(0, 0))
    r.Inflate(25, 25)

    e.Graphics.FillEllipse(Brushes.Red, r)
    e.Graphics.DrawEllipse(Pens.Black, r)
End Sub

The resulting ellipse is badly distorted. And the higher the translation values, the worse the distortion gets.

Does anyone know why GDI+ does this?

Thanks.

¿Fue útil?

Solución

Internally GDI+ uses a fixed point coordinate representation for some operations. You may be running into a 24 bit limit for the integer portion of the coordinates. The work around is to avoid the translation: no one can see anything drawn at those coordinates. If you are doing the translation in order to render to a printer or bitmap, you will need to scale down all your coordinates.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top