Sunday, April 6, 2008

Gdi+ Code Snippets

Gdi+ DrawString, DrawLine and DrawRectangle

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Shalvin.com", Font, Brushes.Blue, 50, 50);
g.DrawLine(Pens.Red , 50, 50, 200, 50);
Rectangle rect = new Rectangle(50, 100, 200,200);
g.DrawRectangle(Pens.Blue, rect);
}

VB .Net
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
g.DrawString("Shalvin.com", Font, Brushes.Blue, 50, 50)
g.DrawLine(Pens.Red, 50, 50, 200, 50)
Dim rect As Rectangle = New Rectangle(50, 100, 200, 200)
g.DrawRectangle(Pens.Blue, rect)
End Sub
























FillEllipse and FillRectangle
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.Blue, 50, 50, 100, 100);
g.FillEllipse(Brushes.Yellow, 50, 150, 150, 200);
}

VB .Net
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
g.FillRectangle(Brushes.Blue, 50, 50, 100, 100)
g.FillEllipse(Brushes.Yellow, 50, 150, 150, 200)
End Sub

DrawPie

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawPie(Pens.Blue, 50, 50, 100, 100, 0, 90);
}

VB .Net
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
g.DrawPie(Pens.Blue, 50, 50, 100, 100, 0, 90)
End Sub





































AntiAlias
using System.Drawing.Drawing2D;

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawLine(Pens.Red, 50, 50, 200, 50);
g.DrawLine(Pens.Blue, 50, 50, 500, 400);
}



















Circle Form with GraphicsPath Class

GraphicsPath is a collection of lines and paths which can be used to draw the outlines of shapes, fill the interiors of shapes, etc.

using System.Drawing.Drawing2D;

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gp;
gp = new GraphicsPath();
gp.AddEllipse(new Rectangle(5, 5, this.Width - 15, this.Height - 15));

this.Region = new Region(gp);
}


VB .Net

Imports System.Drawing.Drawing2D

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim gp As GraphicsPath
gp = New GraphicsPath
gp.AddEllipse(New Rectangle(0, 0, Me.Width - 40, Me.Height - 40))
gp.AddEllipse(New Rectangle(20, 0, 40, Me.Height - 40))
Me.Region = New Region(gp)
End Sub

End Class



Related Blog
Gdi+ Brushes Code Snippets


No comments:

Post a Comment