我的项目中有一张图像存储在Resources/myimage.jpg中。我如何动态加载这张图像到位图对象?


当前回答

我查看了我的一个项目中的设计器代码,注意到它使用了这种符号

myButton.Image = global::MyProjectName.Properties.Resources.max;

其中max是我上传到项目中的资源的名称。

其他回答

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);

或者你可以在处理WPF或Silverlight时使用这一行,特别是当你已经在XAML标记中有源字符串时:

(ImageSource)new ImageSourceConverter().ConvertFromString(ImagePath);

ImagePath是这样的:

string ImagePath  = "/ProjectName;component/Resource/ImageName.png";

比大多数提议的答案简单多了

tslMode.Image = global::ProjectName.Properties.Resources.ImageName;

和ImageBox命名为ImagePreview FormStrings。MyImageNames包含一个常规的get/set字符串强制转换方法,该方法链接到滚动框类型列表。 这些图像的名称与列表中的链接名称相同,除了.bmp结尾。 所有位图都被拖到resources.resx中

Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;

JDS的回答效果最好。加载图像的c#示例:

包括图像作为资源(项目树->资源,右击添加所需的文件ImageName.png) 嵌入式资源(项目树->资源->ImageName.png,右键单击选择属性) .png文件格式(.bmp .jpg也可以)

pictureBox1。Image = ProjectName.Properties.Resources.ImageName;

注意以下几点:

资源映像文件为“ImageName.png”,文件扩展名应省略。 ProjectName也许可以更充分地理解为“程序集名称”,这将是Project->属性页面上各自的文本条目。

使用VisualStudio 2015 Community成功运行示例代码行。