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);
}