[WinForm]How do I configure a button to fit its image ?

  • Thread starter Thread starter Oriane
  • Start date Start date
O

Oriane

Hi there,

a very simple question but I can't find any property in order that the image
set in the Image property of a System.Windows.Forms.Button fits the button.
I have to resize manually the image size if not only part of the image is
shown...

I didn't find something ligne ImageFit...

Best regards

Oriane
 
Dear Oriane,

Based on my understanding, you assign an image to a button by setting the
Image property of the button, but you want the image to fit the size of the
button, similar to a "stretch" layout.

If I misunderstand you, please let me know.

As far as I know, there is no direct way like "ImageLayout" there to set
layout for the button image.

In order to accomplish it, we could use the following ways:

1. Use the BackgroundImage property instead.

Then we can change the image layout by setting the BackgroundImageLayout
property.

For more information about the BackgroundImage property, you can read this
document:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.backgro
undimage.aspx

2. Handle the Button.Paint event to draw the button image at right location.

The following sample demonstrates how to draw the button image to make it
show up in a "stretch" layout style.

void button1_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = e.ClipRectangle;

// Adjust the rectangle for drawing the button image
rect.X += 4;
rect.Y += 4;
rect.Width -= 8;
rect.Height -= 8;
e.Graphics.DrawImage(this.button1.Image, rect);

using (SolidBrush br = new SolidBrush(this.button1.ForeColor))
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(this.button1.Text,
this.button1.Font, br, e.ClipRectangle, sf);
}
}

3. Create a copy of the image with the right size to fit in the button, and
assign this copy to the button Image property instead.

For how to create a copy of an image with a different size, you can read
this document:

How to: Create a Zoom Effect
http://msdn.microsoft.com/en-us/library/ms229648(VS.80).aspx

I hope these alternatives make sense to you. If you need further help on
this, please don't hesitate to let me know.


Best Regards,
Zhi-Xin Ye
Microsoft Newsgroup Support Team


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello,
Zhi-xin Ye said:
Dear Oriane,

Based on my understanding, you assign an image to a button by setting the
Image property of the button, but you want the image to fit the size of
the
button, similar to a "stretch" layout.

If I misunderstand you, please let me know. Your understanding is correct
As far as I know, there is no direct way like "ImageLayout" there to set
layout for the button image.

In order to accomplish it, we could use the following ways:

1. Use the BackgroundImage property instead.
Fine. I take this option !

Thanks a lot
 
Dear Oriane,

You're welcome!

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of help!

Have a good day!

Sincerely,
Zhi-Xin Ye
Microsoft Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top