这篇文章主要介绍了C#编程获取资源文件中图片的方法,涉及C#针对项目中资源文件操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#编程获取资源文件中图片的方法。分享给大家供大家参考。具体实现方法如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Drawing; namespace CL { public class RES { ////// 定义一个资源文件名 资源文件名 = 工程的默认命名空间+文件名(不带扩展名) /// private string PublicResourceFileName = "CL.Resources"; ////// 从资源文件中读取一个资源 /// /// 资源文件名称 命名空间+文件名称 /// 要读取的资源名称 ///返回一个资源 读取失败返回NULL public System.Object ReadFromResourceFile(String resName) { try { Assembly myAssembly; myAssembly = Assembly.GetExecutingAssembly(); System.Resources.ResourceManager rm = new System.Resources.ResourceManager(PublicResourceFileName, myAssembly); return rm.GetObject(resName); } catch (Exception ex) { return null; } } ////// 获取资源图片 /// /// 文件名 ///资源图片 public Bitmap GetResourceImage(String name) { Object tempbitmap = null; tempbitmap = ReadFromResourceFile(name); if (tempbitmap.GetType().Equals(typeof(Bitmap))) { return (Bitmap)tempbitmap; } return null; } } } //调用GetResourceImage方法即可。name为文件的名称不带有后缀.
希望本文所述对大家的C#程序设计有所帮助。
以上就是C#编程获取资源文件中图片的方法的详细内容,更多请关注0133技术站其它相关文章!