none
Rendering issues with Drawline and DrawEllipse RRS feed

  • Question

  • Hi Team,

    I have used below codes to draw a ellipse and tick marks along that ellipse but the drawn circle or lines are not clear it has some glitches at the edge. Could you please look into the code and screenshot and let me know how to draw a circle along with the line without any rendering issue.

    

    Code :

    protected override void OnPaint(PaintEventArgs e)
            {
                const int RADIUS_INNER = 95;
    
                const int RADIUS_CIRCLE = 100;
    
                const int RADIUS_OUTER = 105;
                
                // Draw the circle.
    
                e.Graphics.DrawEllipse(new Pen(Color.Black, 2), 10, 10, 2 * RADIUS_CIRCLE, 2 * RADIUS_CIRCLE);
    
               // Draw the tick marks.
    
                int cx = 10 + RADIUS_CIRCLE;  // Center point.
    
                int cy = 10 + RADIUS_CIRCLE;
    
                double theta = 0;
    
                double dtheta = 30 * Math.PI / 180;
    
                for (int i = 1; i <= 360 / (double)30; i++)
                {
                    int x1 = cx + (int)(Math.Cos(theta) * RADIUS_INNER);
    
                    int y1 = cy + (int)(Math.Sin(theta) * RADIUS_INNER);
    
                    int x2 = cx + (int)(Math.Cos(theta) * RADIUS_OUTER);
    
                    int y2 = cy + (int)(Math.Sin(theta) * RADIUS_OUTER);
    
                    e.Graphics.DrawLine(Pens.Red, x1, y1, x2, y2);
    
                    theta += dtheta;
                }
                base.OnPaint(e);
            }

    Thanks in advance,


    Developer questions


    • Edited by Marimuthu S Tuesday, March 26, 2019 7:04 AM
    Tuesday, March 26, 2019 6:50 AM

All replies

  • Hi,

    You can try to use the property "SmoothingMode" to make the edge smooth.

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            // Code omitted
            // ...
            // Code omitted
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 26, 2019 7:59 AM
    Moderator
  • Hi Kyle,

    I didn't get smooth edges for line even the SmoothingMode is Antialias. Please find the image from below,

    

    There is pixel distortion while drawing line in different angles. Lines which are drawn in 0,90,180 and 270 angles are rendered properly, same as i'm expecting the other lines. How to resolve this?

    Thanks,


    Developer questions

    Tuesday, March 26, 2019 12:25 PM
  • Hi,

    >> There is pixel distortion while drawing line in different angles.

    This seems unavoidable when using "Graphics". You can repeatedly render the incorrect line to darken its color.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 27, 2019 2:28 AM
    Moderator