Chase Ernst
Chase Ernst

Reputation: 1155

Position Background Image

I was wondering if there is any possible way to position a background image on a button in C# windows forms? I am using Visual Studio 2013, and I noticed that you can use the BackgroundImageLayout but that is very limited. I would like to move the background image around by pixel position, or relative to the button. Kind of like this:

enter image description here

I have been on google for a while now with no luck. If anyone could point me in the right direction, or show my an article to read it would be greatly appreciated. Thank you.

Upvotes: 1

Views: 2159

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

You could use the Paint event (or subclass Button to override OnPaint) to draw the image yourself:

private void myButton_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(myImage, myButton.ClientRectangle);
}

You could then use the TextAlign and Padding properties to control the location of the text.

Note that you should not assign the image to the Button's Image or BackgroundImage properties, otherwise .NET will also render the image.

Upvotes: 2

Related Questions