AssemblyHelper.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace UAS_MES_NEW.PublicMethod
  10. {
  11. class AssemblyHelper
  12. {
  13. #region 常量
  14. /// <summary>
  15. /// 程序集的名称
  16. /// </summary>
  17. private static string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
  18. #endregion
  19. #region 变量
  20. /// <summary>
  21. /// 当前程序集
  22. /// </summary>
  23. private static Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
  24. #endregion
  25. #region 方法
  26. /// <summary>
  27. /// 在嵌入的资源文件中查找相应的图片
  28. /// </summary>
  29. /// <param name="name">资源图片的文件名称+扩展名</param>
  30. /// <returns></returns>
  31. public static Image GetImage(string name)
  32. {
  33. Image image = null;
  34. try
  35. {
  36. if (!string.IsNullOrEmpty(name))
  37. {
  38. StringBuilder sb = new StringBuilder();
  39. if (name[0] != '.')
  40. sb.Append(AssemblyHelper.CurrentAssemblyName + "." + name);
  41. else
  42. sb.Append(AssemblyHelper.CurrentAssemblyName + name);
  43. using (Stream stream = CurrentAssembly.GetManifestResourceStream(sb.ToString()))
  44. {
  45. if (stream == null)
  46. throw new Exception("加载资源文件失败,失败原因:可能丢失" + CurrentAssemblyName + ".dll文件。");
  47. else
  48. image = Image.FromStream(stream);
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. Debug.WriteLine("AssemblyHelper.GetImage(string)->" + ex.Message);
  55. throw ex;
  56. }
  57. return image;
  58. }
  59. #endregion
  60. }
  61. }