November 28, 2014
Hot Topics:

Graphics Programming Using C# - Part 2

  • July 31, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Using Advanced Brushes

In this session, we will examine the Linear Gradient Brush and Textured Brush classes.

Using LinearGradientBrush

With the help of the Linear Gradient Brush class, you can render advanced shapes as shown in Listing 4:

Listing 4

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

  public class Gradientbru:Form  {

   public Gradientbru()  {
   this.Text = "Using Solid Brushes";
   this.Paint += new PaintEventHandler(Fill_Graph);
  }

  public void Fill_Graph(object sender,PaintEventArgs e)
  {
   Graphics g = e.Graphics;

   //Creates a Gradient Mode,Brush and fills the rectangle
   /*Various GradientMode values are ForwardDiagonal,
   BackwardDiagonal, Horizontal, Vertical etc. */

   Rectangle r = new Rectangle(3,3,10,10);
   LinearGradientMode lgm =
   LinearGradientMode.ForwardDiagonal;
   LinearGradientBrush lgb = new 
   LinearGradientBrush(r,Color.Blue,Color.Red,lgm);
   g.FillRectangle(lgb,50,50,150,150);
  }

  public static void Main() {
   Application.Run(new Gradientbru());
  }
}

Upon execution, you can view a display as shown in Figure 3.

Figure 3  Output

Using TexturedBrush

Listing 5 examines the usage of Textured Brush. With the help of this Brush class, you can render images instead of patterns as shown below:

Listing 5

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class Texturedbru:Form  {
  Brush bgbrush;

  public Texturedbru()  {
   Image bgimage = new Bitmap("dotnet.gif");
   bgbrush = new TextureBrush(bgimage);
   this.Paint+=new PaintEventHandler(Text_bru);
  }

  public void Text_bru(object sender,PaintEventArgs e) {
   Graphics g = e.Graphics;
   g.FillEllipse(bgbrush,50,50,500,300);
  }

  public static void Main() {
   Application.Run(new Texturedbru());
  }
}

First, an image named dotnet.gif is referred in the object bgimage:

Image bgimage = new Bitmap("dotnet.gif");

Make sure to change the image name if you are choosing your own image.

This object is passed on to create a TextureBrush as shown below:

bgbrush = new TextureBrush(bgimage);

Finally, the object created from it is passed as an argument to the FillEllipse() method:

g.FillEllipse(bgbrush,50,50,500,300);

Figure 4 displays the final output as a result of executing the preceding code.

Figure 4  Output

Working with Images

You can easily render images on a WinForm by using the DrawImage() method of the Graphics class. First, you have to create an object from the image you are going to use as shown below:

Image img = new Bitmap("Image1.bmp");

Finally, you have to apply the above object as shown in the following code fragment:

g.DrawImage(img,20,20,100,90);

Summary

With the help of an example, you have now learned about the application of Pen objects in various DrawXXX() methods. We also looked into a detailed explanation of the code. You also examined the working of two Brush classes—Solid Brush and Hatch Brush. Further, the working of Linear Gradient Brush and Textured Brush were explained in detail. We have also seen the application of Images in C#.

Code Download

You can download the entire code used in this part of the article by clicking here.

About the Author

Anand Narayanaswamy works as a freelance Web developer and technical writer. He also runs and maintains learnxpress.com, and provides free technical support to users worldwide. He can be reached at anand@learnxpress.com.

# # #





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel