我的项目中有一张图片存储在 Resources/myimage.jpg 中。如何将此图像动态加载到 Bitmap 对象中?
16 回答
您在使用 Windows 窗体吗?如果您使用 Properties/Resources UI 添加了图像,您可以从生成的代码中访问图像,因此您可以简单地执行以下操作:
var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage);
您可以通过以下方式获取对图像的引用:
Image myImage = Resources.myImage;
如果要制作图像的副本,则需要执行以下操作:
Bitmap bmp = new Bitmap(Resources.myImage);
完成后不要忘记处理bmp。如果您在编译时不知道资源映像的名称,可以使用资源管理器:
ResourceManager rm = Resources.ResourceManager;
Bitmap myImage = (Bitmap)rm.GetObject("myImage");
ResourceManager 的好处是您可以在 Resources.myImage 通常超出范围或您想要动态访问资源的地方使用它。此外,这适用于声音、配置文件等。
您需要从资源流中加载它。
Bitmap bmp = new Bitmap(
System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.myimage.png"));
如果您想知道程序集中的所有资源名称,请使用:
string[] all = System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceNames();
foreach (string one in all) {
MessageBox.Show(one);
}
比大多数所有建议的答案都容易
tslMode.Image = global::ProjectName.Properties.Resources.ImageName;
最好的办法是将它们作为图像资源添加到项目的资源设置中。然后就可以直接通过Resources.myimage来获取图片了。这将通过生成的 C# 属性获取图像。
如果您只是将图像设置为嵌入式资源,您可以通过以下方式获取它:
string name = "Resources.myimage.jpg"
string namespaceName = "MyCompany.MyNamespace";
string resource = namespaceName + "." + name;
Type type = typeof(MyCompany.MyNamespace.MyTypeFromSameAssemblyAsResource);
Bitmap image = new Bitmap(type.Assembly.GetManifestResourceStream(resource));
其中 MyTypeFromSameAssemblyAsResource 是您在程序集中拥有的任何类型。
我在我的几个项目中使用的代码......它假设您将图像存储在资源中仅作为位图而不是图标
public static Bitmap GetImageByName(string imageName)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resourceName = asm.GetName().Name + ".Properties.Resources";
var rm = new System.Resources.ResourceManager(resourceName, asm);
return (Bitmap)rm.GetObject(imageName);
}
使用以下一种。我已经用 Windows 窗体的 Grid 视图单元对此进行了测试。
Object rm = Properties.Resources.ResourceManager.GetObject("Resource_Image");
Bitmap myImage = (Bitmap)rm;
Image image = myImage;
“Resource_Image”的名称,您可以从项目中找到。
在项目名称下,您可以找到Properties
. 展开它。在那里你可以看到Resources.resx
文件。打开它。将您的文件名应用为“Resource_Image”。
JDS 的回答效果最好。C# 示例加载图像:
- 将图像包含为资源(项目树->资源,右键单击添加所需的文件 ImageName.png)
- 嵌入式资源(项目树->资源->ImageName.png,右键选择属性)
- .png 文件格式(.bmp .jpg 应该也可以)
pictureBox1.Image = ProjectName.Properties.Resources.ImageName;
请注意以下事项:
- 资源图像文件为“ImageName.png”,文件扩展名应省略。
- ProjectName 可能更充分地理解为“程序集名称”,它是 Project->Properties 页面上的相应文本条目。
示例代码行使用 VisualStudio 2015 Community 成功运行。
使用和 ImageBox 命名为 "ImagePreview FormStrings.MyImageNames 包含一个常规的 get/set 字符串转换方法,该方法链接到滚动框类型列表。图像与列表中的链接名称具有相同的名称,但 .bmp 结尾除外。所有位图被拖入resources.resx
Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;
就我而言——我在资源中使用图标,但我需要将它们作为图像动态添加到某些ToolStripMenuItem
(s)中。因此,在我创建的方法中(以下代码片段的来源),我必须先将图标资源转换为位图,然后才能将它们返回以添加到我的MenuItem
.
string imageName = myImageNameStr;
imageName = imageName.Replace(" ", "_");
Icon myIcon = (Icon)Resources.ResourceManager.GetObject(imageName);
return myIcon.ToBitmap();
其他需要注意的事情,如果您的图像/图标在您将它们添加到资源时其名称中有空格(“”),VS 将自动将这些空格替换为“_”(s)。因为,在命名资源时,空格不是有效字符。这就是我Replace()
在引用代码中使用该方法的原因。您可能会忽略该行。
我建议:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
Image yourImage = Image.FromStream(file);
来自 msdn: http: //msdn.microsoft.com/en-us/library/aa287676 (v=vs.71).aspx
使用 Image.FromStream 更好,因为您不需要知道图像的格式(bmp、png、...)。
您还可以将 bmp 保存在 var 中,如下所示:
var bmp = Resources.ImageName;
希望能帮助到你!
奇怪的是,从设计师那里我发现似乎是一种更简单的方法:
该图像似乎可从 .Properties.Resources 获得。
我只是使用图像,因为我感兴趣的只是将其粘贴到带有图像的控件中。
(净 4.0,VS2010。)
我查看了我的一个项目中的设计器代码,发现它使用了这个符号
myButton.Image = global::MyProjectName.Properties.Resources.max;
其中 max 是我上传到项目中的资源的名称。
或者您可以在处理 WPF 或 Silverlight 时使用此行,尤其是在 XAML 标记中已有源字符串的情况下:
(ImageSource)new ImageSourceConverter().ConvertFromString(ImagePath);
ImagePath 类似于:
string ImagePath = "/ProjectName;component/Resource/ImageName.png";
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);