http://www.cnblogs.com/mmbo/archive/2010/02/05/1664454.html
Dotnet中嵌入资源(位图、图标或光标等)有两种方式,一是直接把资源文件加入到项目,作为嵌入资源,在代码中通过Assembly的GetManifestResourceStream方法获取资源的Stream。另一种方法是在项目中加入. resx资源文件,在资源文件中添加资源,由ResourceManager类统一管理其中的资源。
下面分别详述这两种方法
一、 使用GetManifestResourceStream读取嵌入资源
1、 加入资源文件
直接把要嵌入到程序集的资源文件加入到项目中,可以加在项目的根目录,可以加在项目的任何目录中。
2、 设置资源文件的“BuildAction”属性
将嵌入资源文件的“BuildAction”属性设置为“Embedded Resource”
3、 代码中使用嵌入资源
//获得正在运行类所在的名称空间
Type type = MethodBase.GetCurrentMethod().DeclaringType;
string _namespace = type.Namespace;
//获取当前主程序集
Assembly currentAssembly = Assembly.GetExecutingAssembly();
//资源的根名称
string resourceRootName = _namespace + ".ResourceTest";
//实例化资源管理类
ResourceManager resourceManager = new ResourceManager(resourceRootName, currentAssembly);
//根据资源名获得资源对象
Icon myIcon = (Icon)resourceManager.GetObject("IconTest");
2:
//获得正在运行类所在的名称空间
Type type = MethodBase.GetCurrentMethod().DeclaringType;
string _namespace = type.Namespace;
//获得当前运行的Assembly
Assembly _assembly = Assembly.GetExecutingAssembly();
//根据名称空间和文件名生成资源名称
string resourceName = _namespace + ".directory.BitmapManifest.bmp";
//根据资源名称从Assembly中获取此资源的Stream
Stream stream = _assembly.GetManifestResourceStream(resourceName);
Image myImage = Image.FromStream(stream);
3: