Загрузить изображение из ресурсов
Я хочу загрузить изображение следующим образом:
void info(string channel)
{
//Something like that
channelPic.Image = Properties.Resources.+channel
}
Потому что я не хочу делать
void info(string channel)
{
switch(channel)
{
case "chan1":
channelPic.Image = Properties.Resources.chan1;
break;
case "chan2":
channelPic.Image = Properties.Resources.chan2;
break;
}
}
Возможно ли подобное?
Ответы
Ответ 1
Вы всегда можете использовать System.Resources.ResourceManager
, который возвращает кешированный ResourceManager
, используемый этим классом. Поскольку chan1
и chan2
представляют два разных изображения, вы можете использовать System.Resources.ResourceManager.GetObject(string name)
, который возвращает объект, соответствующий вашему вводу, ресурсами проекта
Пример
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
Примечание: Resources.ResourceManager.GetObject(string name)
может возвращать null
, если указанная строка не найдена в ресурсах проекта.
Спасибо,
Надеюсь, вы сочтете это полезным:)
Ответ 2
Вы можете сделать это, используя 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;
}
Ответ 3
Попробуйте это для WPF
StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);
Ответ 4
ResourceManager будет работать, если ваше изображение находится в файле ресурсов. Если это просто файл в вашем проекте (скажем, root), вы можете получить его, используя что-то вроде этого:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = assembly .GetManifestResourceStream("AssemblyName." + channel);
this.pictureBox1.Image = Image.FromStream(file);
Или, если вы находитесь в 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;
}
Ответ 5
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);
Ответ 6
Затем вы можете добавить ресурс изображения в проект (щелкните правой кнопкой мыши по проекту и выберите элемент "Свойства" ), чтобы получить доступ таким образом:
this.picturebox.image = projectname.properties.resources.imagename;