C# 在C中从项目的资源区加载图像#
我的项目中有一个图像存储在Resources/myimage.jpg。如何将此图像动态加载到位图对象中?是否使用Windows窗体?如果您使用属性/资源UI添加了图像,则可以从生成的代码访问图像,因此您只需执行以下操作:C# 在C中从项目的资源区加载图像#,c#,image,winforms,load,C#,Image,Winforms,Load,我的项目中有一个图像存储在Resources/myimage.jpg。如何将此图像动态加载到位图对象中?是否使用Windows窗体?如果您使用属性/资源UI添加了图像,则可以从生成的代码访问图像,因此您只需执行以下操作: var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage); 您可以通过以下方式获取对图像的引用: Image myImage = Resources.myImage; 如果要制作图
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);
}
最好是将它们作为图像资源添加到项目的资源设置中。然后您可以通过执行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));
其中MyTypeFromSameaseMblyaResource是程序集中的任何类型。带有名为“ImagePreview”的ImageBox
FormStrings.MyImageNames包含一个常规的get/set字符串转换方法,该方法链接到scrollbox类型列表。
除了.bmp结尾外,这些图像与列表中的链接名称具有相同的名称。
所有位图都被拖到resources.resx中
Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;
奇怪的是,我从设计师身上发现了一种似乎简单得多的方法: 该映像似乎可从.Properties.Resources获得 我只是简单地使用一个图像,因为我感兴趣的是将它粘贴到一个控件中,控件上有一个图像
(Net4.0,VS2010。)比大多数建议的答案都简单
tslMode.Image = global::ProjectName.Properties.Resources.ImageName;
在我的例子中——我在我的资源中使用了图标,但我需要将它们作为图像动态添加到一些工具条带菜单项中,我必须先将图标资源转换为位图,然后才能返回它们以添加到我的菜单项中
string imageName = myImageNameStr;
imageName = imageName.Replace(" ", "_");
Icon myIcon = (Icon)Resources.ResourceManager.GetObject(imageName);
return myIcon.ToBitmap();
另外需要注意的是,如果将图像/图标添加到资源中时,其名称中有空格(“”),VS将自动将这些空格替换为“uS”。因为,在命名资源时,空格不是有效字符。这就是我使用replace()的原因
方法。您可以忽略该行。我在几个项目中使用的代码。。。
它假定您仅将图像作为位图而不是图标存储在资源中
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);
}
我建议:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
Image yourImage = Image.FromStream(file);
从msdn:
使用Image.FromStream更好,因为您不需要知道图像的格式(bmp、png等)。我查看了一个项目中的设计器代码,发现它使用了这种符号
myButton.Image = global::MyProjectName.Properties.Resources.max;
其中max是我上传到项目中的资源的名称。或者在处理WPF或Silverlight时,您可以使用这一行,特别是在XAML标记中已经有源字符串的情况下:
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);
(ImageSource)new ImageSourceConverter().ConvertFromString(ImagePath);
其中ImagePath类似于:
string ImagePath = "/ProjectName;component/Resource/ImageName.png";
JDS的答案效果最好。C#加载图像示例:
- 将图像作为资源包含(项目树->资源,右键单击以添加所需的文件ImageName.png)
- 嵌入式资源(项目树->资源->ImageName.png,右键单击选择属性)
- .png文件格式(.bmp.jpg也应为OK)
pictureBox1.Image=ProjectName.Properties.Resources.ImageName;
请注意以下几点:
- 资源图像文件为“ImageName.png”,应省略文件扩展名
- ProjectName可以更充分地理解为“程序集名称”,即Project->Properties页面上的相应文本条目
示例代码行使用VisualStudio 2015 Community成功运行。您还可以将bmp保存在一个变量中,如下所示:
var bmp = Resources.ImageName;
希望有帮助!使用下面的一个。我已经用Windows窗体的网格视图单元格测试过了
Object rm = Properties.Resources.ResourceManager.GetObject("Resource_Image");
Bitmap myImage = (Bitmap)rm;
Image image = myImage;
“资源\图像”的名称,您可以从项目中找到
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);
在项目名称下,可以找到属性
。展开它。在那里可以看到资源.resx
文件。打开它。将文件名应用为“资源\u图像”“您不需要使用反射-考虑到可用的简单方法,这似乎过于复杂。此解决方案在C#/Windows Phone.NETCF中不起作用--GetManifestResourceNames()只返回一个名称,即程序集DLL模块的名称。清单中只有一个可用资源——DLL模块的流blob。如果没有C++和不安全的代码,你可以调用Win32 API调用,这是一个巨大的麻烦,因为它可以使用这个内存块。你也可以使用<代码> Type(这个)。汇编< /code >简短。@ SrHARHSHILACKAPAPATA<代码> Type(这个)< /code >将不起作用。code>this.GetType().Assembly
与您所说的最接近,但这是有风险的,因为它会拾取继承的超类,该超类可能位于不同的程序集中。@SriHarshaChilakapatitypeof(this)代码>不编译。您得到的Error 1类型应为
No,它将作为普通文件添加到文件夹资源中。啊,好的。在这种情况下,我认为@Hallgrim已经为您提供了使用资源流的答案。如果您使用的是Windows窗体,我建议您删除图像并从UI中重新添加,这样做会更简单;我正在尝试将资源对象绑定到我的datagrid,但它没有使用您的方法显示。datagrid image列已模板化。请看以下答案:这是一个被问到的问题