Friday, December 09, 2005

.NET Graphics

I saw some enhanced controls created using the “Owner Drawn” feature of Winforms.NET 2.0. So I open the MSDN and start studying System.Drawing form the scratch. I found out that you have to get the Graphics object from the current control to draw graphics in the form and there are 3 methods to do that.

1. Graphic property of the PaintEventArgs, of the Paint event of the control

2. Calling CreateGraphics() method of the control

3. Getting the Graphic object a image using Graphics.FromImage(<class derived from image class>)

To check this I used the onPaint event of the PictureBox control as follows

I saw some enhanced controls created using the “Owner Drawn” feature of Winforms.NET 2.0. So I open the MSDN and start studying System.Drawing form the scratch. I found out that you have to get the Graphics object from the current control to draw graphics in the form and there are 3 methods to do that.

1. Graphic property of the PaintEventArgs, of the Paint event of the control

2. Calling CreateGraphics() method of the control

3. Getting the Graphic object a image using Graphics.FromImage()

To check this I used the onPaint event of the PictureBox control as follows

private void pictureBox1_Paint(object sender, PaintEventArgs e)

{

//Get a graphic object of the picturebox

Graphics graphicsRef = e.Graphics;

//Create a brush

System.Drawing.Brush brush = System.Drawing.Brushes.DarkOrange;

//Draw some text

graphicsRef.DrawString("This is a Sample Text", new Font("Forte", 14), brush, new Point(30, 30));

//Draw a line

graphicsRef.DrawLine(System.Drawing.Pens.BurlyWood, new Point(30, 60), new Point(100, 60));

}

Which generate a output like…













Furhter to add a Liner gradient to the text drawn in the screen I replaced the follwing line

//Create a brush

System.Drawing.Brush brush = System.Drawing.Brushes.DarkOrange;

With the

//Create a linear for the string

System.Drawing.Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(

new Point(30, 30),

new Point(200, 30),

Color.FromArgb(255, 0, 255, 0), // Opaque Green

Color.FromArgb(255, 0, 0, 255)); // Opaque blue

To generate an output like…





No comments: