using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace ButtonImages
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageObject
    {
        /// <summary>
        /// 得到要绘置的图片对像
        /// </summary>
        /// <param name="str">图像在程序集中的地址</param>
        /// <returns></returns>
        public static Bitmap GetResBitmap(string str)
        {
            Stream sm;
            sm = FindStream("", str);
            if (sm == null) return null;
            return new Bitmap(sm);
        }
        /// <summary>
        /// 得到要绘置的图片对像
        /// </summary>
        /// <param name="path">图像在程序集中的路径</param>
        /// <param name="str">图像在程序集中的地址</param>
        /// <returns></returns>
        public static Bitmap GetResBitmap(string path, string str)
        {
            Stream sm;
            sm = FindStream(path, str);
            if (sm == null) return null;
            return new Bitmap(sm);
        }
        /// <summary>
        /// 获取资源图标
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Icon GetResIcon(string str)
        {
            Stream sm;
            sm = FindStream(Application.ExecutablePath, str);
            if (sm == null) return null;
            return new Icon(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(s);
                }
            }
            return null;
        }
        /// <summary>
        /// 得到图程序集中的图片对像
        /// </summary>
        /// <param name="path">图像在程序集中的路径</param>
        /// <param name="str">图像在程序集中的地址</param>
        /// <returns></returns>
        private static Stream FindStream(string path, string str)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            if (path != "" && File.Exists(path))
            { assembly = Assembly.LoadFile(path); }
            string[] resNames = assembly.GetManifestResourceNames();
            foreach (string s in resNames)
            {
                if (s == str)
                {
                    return assembly.GetManifestResourceStream(s);
                }
            }
            return null;
        }
    }
}
