Reader Level:
Articles

Display an Image as Button Background in C#

By Mahesh Chand on May 19, 2010
This code snippet shows how to set an image as background of a Button control using C#.
  • 0
  • 0
  • 43000

The Image property of a Button control is used to set a button background as an image. The Image property needs an Image object. The Image class has a static method called FromFile that takes an image file name with full path and creates an Image object.

You can also align image and text. The ImageAlign and TextAlign properties of Button are used for this purpose.

The following code snippet sets an image as a button background.


// Assign an image to the button.
dynamicButton.Image = Image.FromFile(@"C:\Images\Dock.jpg");
// Align the image and text on the button.
dynamicButton.ImageAlign = ContentAlignment.MiddleRight;
dynamicButton.TextAlign =
ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
dynamicButton.FlatStyle = FlatStyle.Flat;

Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  Mahesh has been awar... Read more

COMMENT USING

Trending up