在 C# 中设置按钮的背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17603272/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:00:07  来源:igfitidea点击:

Setting background image of button in C#

c#imagebuttonbackground

提问by TheMrRafus

I want to set an image as background of a button of Windows Forms.

我想将图像设置为 Windows 窗体按钮的背景。

The image is added to the project′s resources.

图像将添加到项目的资源中。

How do I do this?

我该怎么做呢?

回答by dsfgsho

Simply set the BackgroundImageproperty of the Button. An example:

只需设置按钮的BackgroundImage属性。一个例子:

button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));

回答by Kyle Colantonio

You would use this when you load your application, or whenever you want to set it.

当你加载你的应用程序时,或者你想设置它时,你会使用它。

button.BackgroundImage = YourApplicationNamespace.Properties.Resources.yourImageHere;

回答by Brian

In Visual Studio, you can do this:

在 Visual Studio 中,您可以执行以下操作:

  1. Go to the properties for your button and click the BackgroundImageitem.
  2. Click the '...' icon.
  3. Click the radio button next to 'Local Resource'.
  4. Click 'Import' and select the item you wish to have as the background.
  5. Then, click 'Ok'.
  1. 转到按钮的属性并单击该BackgroundImage项目。
  2. 单击“...”图标。
  3. 单击“本地资源”旁边的单选按钮。
  4. 单击“导入”并选择您希望作为背景的项目。
  5. 然后,单击“确定”。

Or, from code you can do this:

或者,您可以从代码中执行以下操作:

button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));