这篇文章给大家分享的是有关C#编程如何获取资源文件中图片的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体内容如下:
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;
namespace CL
{
public class RES
{
/// <summary>
/// 定义一个资源文件名 资源文件名 = 工程的默认命名空间+文件名(不带扩展名)
/// </summary>
private string PublicResourceFileName = "CL.Resources";
/// <summary>
/// 从资源文件中读取一个资源
/// </summary>
/// <param name="resFile">资源文件名称 命名空间+文件名称</param>
/// <param name="resName">要读取的资源名称</param>
/// <returns>返回一个资源 读取失败返回NULL</returns>
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;
}
}
/// <summary>
/// 获取资源图片
/// </summary>
/// <param name="name">文件名</param>
/// <returns>资源图片</returns>
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#编程如何获取资源文件中图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!