如何从 C# 中的资源文件设置面板的背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16387302/
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
How to set the background image of a panel from a resource file in C#?
提问by Hussain
I want to change the background image of a panel in a C# Windows Formsapplication. The image I want to set as the background is located in a resource folder in Solution Explorer. How do I use it in code?
我想更改 C# Windows 窗体应用程序中面板的背景图像。我想设置为背景的图像位于解决方案资源管理器中的资源文件夹中。我如何在代码中使用它?
I tried this:
我试过这个:
panel1.BackgroundImage = Properties.Resources.Chalkboard;
But it didn't work.
但它没有用。
采纳答案by Joshua Maerten
I tried the same code like you did, and it works fine when I hit a button.
我像你一样尝试了相同的代码,当我按下按钮时它工作正常。
private void pnlBgBtn_Click(object sender, EventArgs e)
{
panel1.BackgroundImage = Properties.Resources.image;
}
The name 'image' in 'Properties.Resources.image' should be the name you gave to the image. The right name of the image should be the name shown in your project properties under project-proje.
'Properties.Resources.image' 中的名称 'image' 应该是您为图像指定的名称。图像的正确名称应该是在 project-proje 下的项目属性中显示的名称。
回答by Blablablaster
You can create an icon resource in Properties project folder. When you opened Properties click on Resources.resx and there Add Resource->Add New Icon menu items. This will create an icon. You can also load an icon from an existing file into the resource, in this case the icon will be built in your executable. So, when your icon added as a resource it will be given some name.
您可以在 Properties 项目文件夹中创建图标资源。当您打开属性时,单击 Resources.resx,然后添加资源->添加新图标菜单项。这将创建一个图标。您还可以将现有文件中的图标加载到资源中,在这种情况下,图标将构建在您的可执行文件中。因此,当您的图标作为资源添加时,它会被赋予一些名称。
回答by Santosh Panda
You can try this out:
你可以试试这个:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.myimage.png"));
panel1.BackgroundImage = bmp;
回答by Neeraj Dubey
If you want to set the background image of a panel on page load then you have to write this code:
如果要在页面加载时设置面板的背景图像,则必须编写以下代码:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Assembly asm = Assembly.GetExecutingAssembly();
Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("Image913.jpg"));
e.Graphics.DrawImage(
backgroundImage,
this.ClientRectangle,
new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height),
GraphicsUnit.Pixel);
}
If you want set the image except the panel, load use this code:
如果要设置面板以外的图像,请使用以下代码加载:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.photo0018.jpg.png"));
panel1.BackgroundImage = bmp;
回答by Douglas David
The properties.Resources class doesn't return every resourse as an image so you have to apply a cast to Image like this
properties.Resources 类不会将每个资源作为图像返回,因此您必须像这样对 Image 应用强制转换
panel1.BackgroundImage = (Image)(Properties.Resourses.Chalkboard);

