Thursday, September 24, 2009

Gdi+ in Asp .Net Code Snippets

This blog is the continuation of Gdi+ Code Snippets
and Gdi+ Brushes Code Snippets

using System.Drawing.Imaging;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Yellow );
g.DrawLine(p, 5, 50, 50, 5);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}
}

Custom Font

using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);

Font f = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold ^ FontStyle.Italic );

g.DrawString("Shalvin.com", f, Brushes.White, 50, 50);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
}

Drawing a Pie Chart

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(200, 200);
        
        Rectangle r = new Rectangle(0, 0, 200, 200);
        
        Graphics g = Graphics.FromImage(bmp);
        g.SmoothingMode = SmoothingMode.AntiAlias;

        g.FillRectangle(Brushes.White, r);

        //25%
        g.FillPie(Brushes.LightBlue, r, 0, 90);

        //50%
        g.FillPie(Brushes.LightGreen, r, 90, 180);
        
        //25%
        g.FillPie(Brushes.LightGray, r, 270, 90);

        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

    }
}


Pie Chart Asp .Net Gdi+

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

Saturday, September 12, 2009

Asp .Net Themes and Skins

Asp .Net Themes can be used for getting a consistent look and feel for Asp .Net Controls.

Add a skin file the existing project by selecting Skin from the Add New Item Dialog.



By default themes are added to Asp .Net folder called App_Thmes.



Inside a skin file you can specify the way in which Asp .Net control should render by specifying the properties as attributes to Asp .Net Control tags.



For applying a theme to web page go to document's Theme property and select the theme which you have defined.



Since I have specified properties for TextBox and Label the TextBoxes and Labels will inherit those properties.