首页 > 代码库 > C# 从类库中获取资源图片,把图片资源保存到类库中

C# 从类库中获取资源图片,把图片资源保存到类库中

/// <summary>
    /// 获取资源图片
    /// </summary>
    public 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 img = 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 (System.IO.Stream stream = CurrentAssembly.GetManifestResourceStream(sb.ToString()))
                    {
                        if (stream != null)
                        {
                            img = Image.FromStream(stream);
                        }
                    }
                }
            }
            catch (Exception ex) { }
            return img;
        }
        #endregion
    }

 

C# 从类库中获取资源图片,把图片资源保存到类库中