C# 使用按钮大小制作背景图像比例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13357032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a background image scale with button size
提问by Travis
I'm trying to add some background images to a few buttons in my Win Forms application. The three images are different sizes (ie pixel dimensions don't match, one is 128x128 and another is 256x256). I need the buttons to be identical in size (otherwise the GUI is horribly asymmetrical). Without changing the actual image files, how can I get the images to scale with button size?
我正在尝试向 Win Forms 应用程序中的几个按钮添加一些背景图像。三张图片大小不同(即像素尺寸不匹配,一张是128x128,另一张是256x256)。我需要按钮的大小相同(否则 GUI 非常不对称)。在不更改实际图像文件的情况下,如何使图像按按钮大小进行缩放?
I've tried creating my own class, and adding an event handler for the button resize event, but that doesn't seem to work. My code:
我尝试创建自己的类,并为按钮调整大小事件添加一个事件处理程序,但这似乎不起作用。我的代码:
class CustomButton : Button {
internal void CustomButton_Resize( object sender, EventArgs e ) {
if ( this.BackgroundImage == null ) {
return;
}
var pic = new Bitmap( this.BackgroundImage, this.Width, this.Height );
this.BackgroundImage = pic;
}
}
and in the form:
并以以下形式:
this.buttonOne.Resize += new System.EventHandler(this.buttonOne.CustomButton_Resize);
Forgot to mention, the above code does not resize the images at all. The buttons still need to have different sizes to display the images completely.
忘了提,上面的代码根本没有调整图像大小。按钮仍然需要有不同的大小才能完整显示图像。
采纳答案by Sami
The easy programmatic way
简单的编程方式
Say I have a button btn1, Following code is working perfectly in visual-studio-2010.
假设我有一个按钮btn1,以下代码在 visual-studio-2010 中完美运行。
private void btn1_Click(object sender, EventArgs e)
{
btn1.Width = 120;
btn1.Height = 100;
}
void btn1_Resize(object sender, EventArgs e)
{
if ( this.BackgroundImage == null )
return;
var bm = new Bitmap(btn1.BackgroundImage, new Size(btn1.Width, btn1.Height));
btn1.BackgroundImage = bm;
}
The better way
更好的方法
You can add eventHandler in the constructor of your custombutton (just to ensure that you are adding eventhandler correctly)
您可以在自定义按钮的构造函数中添加 eventHandler (只是为了确保您正确添加了 eventhandler)
class CustomButton : Button
{
CustomButton()
{
this.Resize += new System.EventHandler(buttonOne.CustomButton_Resize);
}
void CustomButton_Resize( object sender, EventArgs e )
{
if ( this.BackgroundImage == null )
return;
var pic = new Bitmap( this.BackgroundImage, new Size(this.Width, this.Height) );
this.BackgroundImage = pic;
}
}
Now when you will resize the button anywhere your image will get fit(scaled) to its new size.
现在,当您在任何位置调整按钮大小时,您的图像将适合(缩放)到其新大小。
回答by Alan
You could start with something like this...
你可以从这样的事情开始......
public class ImageButton : Control
{
public Image backgroundImage;
public Image BackgroundImage
{
get
{
return backgroundImage;
}
set
{
backgroundImage = value;
Refresh();
}
}
public ImageButton()
{
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
if(BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, 0, 0, Width, Height);
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//base.OnPaintBackground(pevent);
}
}
You can just handle paint and draw the image yourself. You may also try using a PictureBox or some other control which has more scaling options
您可以自己处理油漆并绘制图像。您也可以尝试使用 PictureBox 或其他具有更多缩放选项的控件
回答by gonzobrains
Easiest way to add a background image to a .NET Button object and scale it to fit
将背景图像添加到 .NET Button 对象并对其进行缩放以适合的最简单方法
I used this method to avoid any additional coding of new classes and event handlers. This helped me also avoid converting all Button objects into Image objects.
我使用这种方法来避免对新类和事件处理程序进行任何额外的编码。这也帮助我避免将所有 Button 对象转换为 Image 对象。
Add image to your Resources.resx file.
Click on your chosen button.
Navigate to the
BackgroundImageproperty and choose the image you imported into the project's resources.resx file.Navigate to the
BackgroundImageLayoutproperty and chooseStretch.
将图像添加到 Resources.resx 文件。
单击您选择的按钮。
导航到该
BackgroundImage属性并选择您导入到项目的 resources.resx 文件中的图像。导航到该
BackgroundImageLayout属性并选择Stretch。
Make sure you don't have anything entered for the Imageand Textproperties or else they will interfere with your new background image.
确保您没有为Image和Text属性输入任何内容,否则它们会干扰您的新背景图像。

