Posts

Showing posts with the label 2d Drawing

C# Anti Aliasing when drawing

Image
In a previous post I talked about how to set anti-aliasing on text in C#. Now you may find yourself needing to set anti aliasing when drawing shapes. This is very easy to do using the GDI+ graphics object. The trick is to use the SmoothingMode and PixelOffsetMode . Now some quick code protected override void OnPaint( PaintEventArgs e ) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ), 1.0f ), new Rectangle( 5, 5, 200, 200 ) ); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ), 1.0f ), new Rectangle( 210, 5, 200, 200 ) ); e.Graphics.SmoothingMode = SmoothingMode.HighSpeed; e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; e.Graphics.DrawEllipse( new Pen( new SolidBrush( Color.Red ),...

Antialiasing Text with C#

Image
Lately I have found myself having to use anti-aliasing in a lot of our rendering code to improve the output look our images.  To help others here is a snippet of code to anti alias text. protected override void OnPaint( PaintEventArgs e ) {     base.OnPaint( e );     Font font = new Font( "Tahoma", 12.0f, FontStyle.Regular );     e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;     TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 20 ), Color.Black );      e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;      TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 40 ), Color.Black );     e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;     TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 60 ), Color.Black );     e....