如何在资源中添加此图像? [英] how to add this image in resource ?

查看:67
本文介绍了如何在资源中添加此图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ZedGraph上的一个演示项目,并在图像上出错。我检查了两个图像的属性Build Action,它们都是Emdedded Resource。



代码结构如下:

I am working on a demo project on ZedGraph and get error on images. I checked the property Build Action of the two images, they are both "Emdedded Resource".

the code structure is as below:

namespace ZedGraph.Demo
{
    /// <summary>
    /// Summary description for TransparentDemo1.
    /// </summary>
    public class TransparentDemo1 : DemoBase
    {
        public TransparentDemo1() : base( "A transparency demo", "Transparency Demo 1", DemoType.Special )
        {
....................................
			// Get an image for the background
			Image image = Bitmap.FromStream(GetType().Assembly.GetManifestResourceStream("ZedGraph.Demo.ngc4414.jpg"));
.....................................................
}

when I compile this project, always get error on this GetManifestResourceStream() statement:
Value of 'null' is not valid for 'stream'.

please help me on this issue related with embedding this image into resource file.



.........


.........

推荐答案

为了合理地使用资源,尤其是像JPEG这样的资源,您不需要知道任何资源名称,使用任何类似的流。一切都将为您完成。



在某个目录中创建一个新资源,例如Images.resx。有一些准备使用JPEG文件。打开创建的资源并使用添加资源 - > 添加现有文件,添加您的文件。将生成一个新文件Images.Designer.cs。在此文件中打开。您将找到类型为 System.Drawing.Bitmap 的静态属性,并且该属性的名称将接近原始JPEG文件的名称。直接使用它。你不需要打开任何东西,阅读,关闭 - 没有。只需使用该对象。您可以期待与其他众所周知的文件类型类似的行为:文本,其他图像等等。如果文件类型不为人所知,您可以从内存流中读取它,而不是资源流。



小心不要在项目中添加两次文件。在此过程中,您的文件将被复制到您的项目目录或相应的子目录,引用它添加到项目中(具有复制属性不复制,因为它将被嵌入),以及参考添加到资源。如果此文件已在项目目录结构下,但未放在应添加到资源的确切位置,则可能会出现重复副本,因此请进行检查。您可以随时以相同的名称替换该文件,然后重建项目。



这就是全部。



-SA
To use resources reasonably, especially such resources as JPEG, you don't need to know any resource names, use any streams nothing like that. Everything will be done for you.

Create a new resource, say, Images.resx in in some directory. Have some ready to use JPEG file. Open the created resource and use "Add Resource" -> "Add Existing File", add your file. A new file will be generated "Images.Designer.cs". Open in this file. You will find a static property of the type System.Drawing.Bitmap, and the name of the property will be close to the name of the original JPEG file. Use it directly. You don't need to open anything, read, close — nothing. Just use the object. You can expect similar behavior with other well-known file types: text, other images, and a lot more. If the file type is not well-known, you can read it from memory stream, not resource stream.

Be careful not to add a file twice in your project. During this process, your file will be copied to your project directory or appropriate sub-directory, reference it added to the project (with the "copy" attribute "Do not Copy", because it is going to be embedded), and a reference added to the resource. If this file is already under your project directory structure but not at the exact location where it should be placed for adding to the resource, a duplicate copy may appear, so check it up. You can replace the file at any time, under the same name, and rebuild the project.

That's all.

—SA


你应该得到正在执行的程序集,然后尝试从流中获取资源。



此外,资源位置也必须正确。

You should get the executing assembly, then try to get the resource from the stream.

Also that resource location need to be right too.
System.Reflection.Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("ZedGraph.Demo.ngc4414.jpg");

// If the resource is not present it will be null or empty
if (null != file) {
    Image image = Image.FromStream(file);
}


这篇关于如何在资源中添加此图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆