C# 从资源加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13592150/
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
Load image from resources
提问by a1204773
I want to load the image like this:
我想像这样加载图像:
void info(string channel)
{
//Something like that
channelPic.Image = Properties.Resources.+channel
}
Because I don't want to do
因为我不想做
void info(string channel)
{
switch(channel)
{
case "chan1":
channelPic.Image = Properties.Resources.chan1;
break;
case "chan2":
channelPic.Image = Properties.Resources.chan2;
break;
}
}
Is something like this possible?
这样的事情可能吗?
采纳答案by Picrofo Software
You can always use System.Resources.ResourceManagerwhich returns the cached ResourceManagerused by this class. Since chan1and chan2represent two different images, you may use System.Resources.ResourceManager.GetObject(string name)which returns an object matching your input with the project resources
您始终可以使用System.Resources.ResourceManagerwhich 返回ResourceManager此类使用的缓存。由于chan1和chan2代表两个不同的图像,您可以使用System.Resources.ResourceManager.GetObject(string name)which 返回与您的输入与项目资源匹配的对象
Example
例子
object O = Resources.ResourceManager.GetObject("chan1"); //Return an object from the image chan1.png in the project
channelPic.Image = (Image)O; //Set the Image property of channelPic to the returned object as Image
Notice: Resources.ResourceManager.GetObject(string name)may return nullif the string specified was not found in the project resources.
注意:如果在项目资源中找不到指定的字符串,则Resources.ResourceManager.GetObject(string name)可能会返回null。
Thanks,
I hope you find this helpful :)
谢谢,
我希望你觉得这有帮助:)
回答by huysentruitw
You can do this using the ResourceManager:
您可以使用以下方法执行此操作ResourceManager:
public bool info(string channel)
{
object o = Properties.Resources.ResourceManager.GetObject(channel);
if (o is Image)
{
channelPic.Image = o as Image;
return true;
}
return false;
}
回答by xr280xr
ResourceManager will work if your image is in a resource file. If it is just a file in your project (let's say the root) you can get it using something like this:
如果您的图像在资源文件中,ResourceManager 将起作用。如果它只是您项目中的一个文件(假设是根),您可以使用以下内容获取它:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = assembly .GetManifestResourceStream("AssemblyName." + channel);
this.pictureBox1.Image = Image.FromStream(file);
Or if you're in WPF:
或者,如果您在 WPF 中:
private ImageSource GetImage(string channel)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/TestApp;component/" + channel, UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = sri.Stream;
bmp.EndInit();
return bmp;
}
回答by Roopsundar
Try this for WPF
试试这个 WPF
StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);
回答by HamidReza
You can add an image resource in the project then (right click on the project and choose the Propertiesitem) access that in this way:
您可以在项目中添加图像资源,然后(右键单击项目并选择属性项)以这种方式访问它:
this.picturebox.image = projectname.properties.resources.imagename;
回答by IR.Programmer
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(444, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
object O = global::WindowsFormsApplication1.Properties.Resources.ResourceManager.GetObject("best_robust_ghost");
ToolStripButton btn = new ToolStripButton("m1");
btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
btn.Image = (Image)O;
this.toolStrip1.Items.Add(btn);
this.Controls.Add(this.toolStrip1);

