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_NEW.PublicMethod { class AssemblyHelper { #region 常量 /// /// 程序集的名称 /// private static string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name; #endregion #region 变量 /// /// 当前程序集 /// private static Assembly CurrentAssembly = Assembly.GetExecutingAssembly(); #endregion #region 方法 /// /// 在嵌入的资源文件中查找相应的图片 /// /// 资源图片的文件名称+扩展名 /// 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 } }