软件编程
位置:首页>> 软件编程>> C#编程>> C#编程获取资源文件中图片的方法

C#编程获取资源文件中图片的方法

作者:我心依旧  发布时间:2023-05-23 06:13:39 

标签: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
 {
   /// <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#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com