Net Core教程

C# 获取Resources

本文主要是介绍C# 获取Resources,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


        /// <summary>
        /// 得到要绘置的图片对像
        /// </summary>
        /// <param name="str">图像在程序集中的地址</param>
        /// <returns></returns>
        public static Bitmap GetResBitmap(string str)
        {
           
            Stream sm = FindStream(str);
            if (sm == null) return null;
            return new Bitmap(sm);
        }

        /// <summary>
        /// 得到图程序集中的图片对像
        /// </summary>
        /// <param name="str">图像在程序集中的地址</param>
        /// <returns></returns>
        private static Stream FindStream(string str)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string[] resNames = assembly.GetManifestResourceNames();            
            foreach (string s in resNames)
            {
                if (s == str)
                {
                    return assembly.GetManifestResourceStream(str);
                }
            }
            return null;
        }
  /// <summary>
        /// 获取图像
        /// </summary>
        public static Image GetBitmap(string name)
        {
            object obj = Properties.Resources.ResourceManager.GetObject(name,Properties.Resources.Culture);
            return (Image)obj;
        }

这篇关于C# 获取Resources的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!