Showing posts with label Code Snippets. Show all posts
Showing posts with label Code Snippets. Show all posts

Thursday, September 24, 2009

Gdi+ Brushes Code Snippets

This blog is the continuation of Gdi+ Code Snippets

HatchBrush
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
HatchBrush hb;
hb = new HatchBrush(HatchStyle.DarkHorizontal, Color.Red);
g.FillRectangle(hb, 50, 50, 100, 100);
hb = new HatchBrush(HatchStyle.Cross, Color.Blue, Color.Brown);
g.FillRectangle(hb, 100, 100, 150, 150);
hb = new HatchBrush(HatchStyle.DottedDiamond, Color.Green, Color.Blue);
g.FillRectangle(hb, 150, 150, 200, 200);
}

VB .Net
Imports System.Drawing.Drawing2D
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
Dim hb As HatchBrush
hb = New HatchBrush(HatchStyle.DarkHorizontal, Color.Red)
g.FillRectangle(hb, 50, 50, 100, 100)
hb = New HatchBrush(HatchStyle.Cross, Color.Blue, Color.Brown)
g.FillRectangle(hb, 100, 100, 150, 150)
hb = New HatchBrush(HatchStyle.DottedDiamond, Color.Green, Color.Blue)
g.FillRectangle(hb, 150, 150, 200, 200)
End Sub























LinearGradientBrush

using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new LinearGradientBrush(new Rectangle(50, 50, 100, 100), Color.Red, Color.Blue, 0, false );
g.FillRectangle(b, 50, 50, 100, 100);
}

VB .Net


Imports System.Drawing.Drawing2D

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
Dim b As New LinearGradientBrush(New Rectangle(50, 50, 100, 100), Color.Red, Color.Blue, 0, False)
g.FillRectangle(b, 50, 50, 100, 100)
End Sub




Setting LinearGradientBrush as Background for Form

using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height ), Color.Red, Color.Blue, 0, false);
g.FillRectangle(b, 0, 0, this.Width, this.Height);
}

VB .Net
Imports System.Drawing.Drawing2D
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
Dim b As Brush = New LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), Color.Red, Color.Blue, 0, False)
g.FillRectangle(b, 0, 0, Me.Width, Me.Height)
End Sub













Color.FromArgb
private void btnRed_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 0, 0);
}

private void btnGreen_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 255, 0);
}

private void btnBlue_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 0, 255);

}

private void btnYellow_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 255, 0);
}

private void btnWhite_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(255, 255, 255);
}

private void btnBlack_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 0, 0);
}

Exploring Random Class

Random rnd = new Random();
private void btnBigNumber_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next().ToString();
}

private void btnL20_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next(10).ToString();
}

private void btn10To20_Click(object sender, EventArgs e)
{
lblValue.Text = rnd.Next(10, 20).ToString();
}

Random Color ScreenSaver

This example makes use of a Timer control.

private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
this.BackColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
}

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Close();
}
















Random Multicolor Rectangle Screen Saver and Invalidate Method

Invalidate method is used for a forced repainting of the window. Here I am using Invalidate method in the Timer's Tick event to redraw the rectangle based on the randomized values .

Random rnd = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush b = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
g.FillRectangle(b , rnd.Next(this.Width), rnd.Next(this.Height), rnd.Next(this.Width), rnd.Next(this.Height));
}

Custom Pen

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Brushes.Blue, 3);
g.DrawLine(p, 50, 50, 200, 50);
}

Working with Bitmaps

private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp;
bmp = new Bitmap("d:\\ShalvinSmall.jpg");
Graphics g = e.Graphics;
g.DrawImage(bmp, 50, 50);
}

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