Marek
Marek

Reputation: 3575

Align button background image

I have a button in my WinForms app and I added an image and text to it. I aligned the text to right and wanted to align the Background image to left but found out that it is not possible.

Is there any way to do that?

I have also tried to set just Image on the button but that couldn't be resized in the Button Properties.

May someone help me solve this out? Thanks so much.

In case that it is not possible I would have to resize every image in mspaint.

This is the result (as Background):

enter image description here

I need the BackgroundImage align to left.

This is result as Image when using align (not possible to resize)

enter image description here

Upvotes: 4

Views: 15699

Answers (3)

Irfan
Irfan

Reputation: 2771

Set these properties of Button.

ImageAlign to MiddleRight
TextImageRelation to ImageBeforeText
TextAlign as MiddleCenter

To have it resized on Button. See below:

Bitmap image = Bitmap.FromFile(oFile) as Bitmap;
Bitmap resized = new Bitmap(image, new Size(30, 30));
button1.Image = resized;
button1.Text = "Button";
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;

Upvotes: 6

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

  1. Use Image property to set your image (make sure it fits button height, you can change image size if you will open it from project resources folder)
  2. Set ImageAlign to MiddleLeft
  3. Set TextAlign to MiddleRight

Do not change anything else. I.e. TextImageRelation should be Overlay. Result:

enter image description here

Upvotes: 8

Moslem Ben Dhaou
Moslem Ben Dhaou

Reputation: 7005

You can use the Image property instead of the BackgroundImage. You can set the align later using the ImageAlign property.

Upvotes: 2

Related Questions