1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace UAS_MES.PublicMethod
- {
- class AssemblyHelper
- {
- #region 常量
- /// <summary>
- /// 程序集的名称
- /// </summary>
- private static string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
- #endregion
- #region 变量
- /// <summary>
- /// 当前程序集
- /// </summary>
- private static Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
- #endregion
- #region 方法
- /// <summary>
- /// 在嵌入的资源文件中查找相应的图片
- /// </summary>
- /// <param name="name">资源图片的文件名称+扩展名</param>
- /// <returns></returns>
- public static Image GetImage(string name)
- {
- Image image = null;
- try
- {
- if (!string.IsNullOrEmpty(name))
- {
- StringBuilder sb = new StringBuilder();
- if (name[0] != '.')
- sb.Append(AssemblyHelper.CurrentAssemblyName + "." + name);
- else
- sb.Append(AssemblyHelper.CurrentAssemblyName + name);
- using (Stream stream = CurrentAssembly.GetManifestResourceStream(sb.ToString()))
- {
- if (stream == null)
- throw new Exception("加载资源文件失败,失败原因:可能丢失" + CurrentAssemblyName + ".dll文件。");
- else
- image = Image.FromStream(stream);
- }
- }
- }
- catch (Exception ex)
- {
- Debug.WriteLine("AssemblyHelper.GetImage(string)->" + ex.Message);
- throw ex;
- }
- return image;
- }
- #endregion
- }
- }
|