Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align button background image

Tags:

c#

winforms

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

like image 720
Marek Avatar asked Dec 05 '22 09:12

Marek


2 Answers

  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

like image 67
Sergey Berezovskiy Avatar answered Dec 23 '22 15:12

Sergey Berezovskiy


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;
like image 23
Irfan Avatar answered Dec 23 '22 15:12

Irfan