如何以编程方式更改表单 C# 上的背景图像

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

how to programmatically change the background image on form c#

c#winformsbackground-image

提问by Darkmage

I need to change the background image of my form when i klick a button, and change it back to null again the second time it is clicked, how can i do this?

我需要在单击按钮时更改表单的背景图像,并在第二次单击时再次将其更改回 null,我该怎么做?

采纳答案by Micha? Ziober

Use BackgroundImage property:

使用 BackgroundImage 属性:

form.BackgroundImage = image;

to hide the image:

隐藏图像:

form.BackgroundImage = null;

Put this source code to ClickButton method:

将此源代码放入 ClickButton 方法:

form.BackgroundImage = form.BackgroundImage == null ? image : null;

回答by treaschf

You should be able to set the BackgroundImage property of your form from the event handler of that button.

您应该能够从该按钮的事件处理程序设置表单的 BackgroundImage 属性。

For example you could do it like this:

例如,你可以这样做:

this.BackgroundImage = new Bitmap(@"c:\Temp\image.bmp");

this.BackgroundImage = new Bitmap(@"c:\Temp\image.bmp");

In order to remove the image, set the property back to null.

为了删除图像,将该属性设置回 null。

The image can also come from a resource.

图像也可以来自资源。