我的项目中存有一个存储在Resources/myimage.jpg中的图像。如何将此图像动态加载到Bitmap对象中?
您使用的是Windows Forms吗?如果您使用“属性/资源”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;
最好的方法是在Project的Resources设置中将它们添加为Image Resources。然后,您可以通过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是您在Assembly中拥有的任何类型。
代码我在我的几个项目中使用...它假设您将图像作为位图而不是图标存储在资源中
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);
}
使用和ImageBox命名为“ImagePreview FormStrings.MyImageNames包含一个常规的get/set字符串强制转换方法,它们链接到一个滚动条类型列表。这些图像与列表中的链接名称具有相同的名称,但.bmp结尾除外。位图被拖入resources.resx
Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;
在我的情况下 - 我在我的资源中使用 Icons 但我需要将它们动态添加为 Images 添加到某些ToolStripMenuItem
(s)。所以在我创建的方法(下面的代码片段来自哪里)中,我必须将图标资源转换为位图,然后才能返回它们以添加到我的MenuItem
。
string imageName = myImageNameStr;
imageName = imageName.Replace(" ", "_");
Icon myIcon = (Icon)Resources.ResourceManager.GetObject(imageName);
return myIcon.ToBitmap();
还有一点要注意,如果你的图像/图标在你的资源中添加了它们的名字时会有空格(“”),VS会自动用“_”代替这些空格。因为,在命名资源时,空格不是有效字符。这就是为什么我在引用的代码中使用Replace()
方法。您可能只是忽略该行。
使用下面一个。我用Windows窗体的网格视图单元测试了这个。
Object rm = Properties.Resources.ResourceManager.GetObject("Resource_Image");
Bitmap myImage = (Bitmap)rm;
Image image = myImage;
您可以从项目中找到“Resource_Image”的名称。
在项目名称下,您可以找到Properties
。展开它。在那里你可以看到Resources.resx
文件。打开它。将您的文件名应用为“Resource_Image”。
JDS的答案效果最好。 C#示例加载图片:
pictureBox1.Image = ProjectName.Properties.Resources.ImageName;
请注意以下事项:
使用VisualStudio 2015社区成功运行示例代码行。
我建议:
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";