Showing posts with label LinearGradientBrush. Show all posts
Showing posts with label LinearGradientBrush. 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);
}

Tuesday, July 28, 2009

WPF Colors

The Brushes class provides 141 colors.

private void btnColor_Click(object sender, RoutedEventArgs e)
{
this.Background = Brushes.Red;
}


You can also have a wide ranging colors using the combination of Red, Green and Blue.

private void btnGreen_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromRgb(0, 255, 0));
}

In the previous I am creating Green color by assigning 255, the maximum value to Green component, ie. the second parameter


private void btnWhite_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
}

private void btnYellow_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromRgb(255, 255, 0));
}

Grey Scale
private void btnWhite_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
}

private void btnBlack_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
}

private void btnDarkGrey_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50));
}

private void btnMGrey_Click(object sender, RoutedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(255, 150,150, 150));
}

private void slider1_ValueChanged(object sender,

RoutedPropertyChangedEventArgs e)
{
this.Background = new SolidColorBrush(Color.FromArgb(255,

(byte)slider1.Value, (byte)slider1.Value, (byte)slider1.Value));
}


LinearGradientBrush
LinearGradientBrush displays a gradualy changing mix of two or more color schemes.
The surface where you are planning to draw the linear gradient is considered to he unit 1 wide and 1 unit high.


private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Background = new LinearGradientBrush(Colors.Red, Colors.Blue, new Point(0, 0), new Point(1, 1));
}


LinearGradientBrush the Coded way


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFBrush
{
class Class1 : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new Class1 { Title = "Shalvin - Colors" });
}
public Class1()
{
Grid LayoutRoot = new Grid();
this.Content = LayoutRoot;
LayoutRoot.Background = new LinearGradientBrush(Colors.Red, Colors.Blue, 0);
}
}
}


RadialGradientBrush

Grid LayoutRoot = new Grid();
this.Content = LayoutRoot;
LayoutRoot.Background = new RadialGradientBrush (Colors.Red, Colors.Blue);


RadialGradientBrush and GradientStops


RadialGradientBrush brush = new RadialGradientBrush();
Background = brush;
brush.GradientStops.Add(new GradientStop(Colors.Red, 0));
brush.GradientStops.Add(new GradientStop(Colors.Green, .33));
brush.GradientStops.Add(new GradientStop(Colors.Blue, .66));