您当前的位置: 首页 > 

衣舞晨风

暂无认证

  • 0浏览

    0关注

    1156博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

WinForm 加载本地图片

衣舞晨风 发布时间:2014-12-24 21:27:41 ,浏览量:0

方式一:

 /// 
        /// 从程序集中加载图片
        /// 
        /// 图片路径(该图片必须是嵌入的资源)
        /// Bitmap 格式图片
        public Bitmap GetIcon(string imagePathName)
        {
            Bitmap b = null;
            try
            {
                Assembly asm1 = Assembly.GetExecutingAssembly();
                string name = asm1.GetName().Name;
                asm1.GetManifestResourceStream(imagePathName);
                Stream imageStream = asm1.GetManifestResourceStream(imagePathName);
                if (imageStream != null)
                {
                    b = new Bitmap(Image.FromStream(imageStream));
                    b.MakeTransparent();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return b;
        }

小注:
      1、 imagePathName= Assembly.GetExecutingAssembly().GetName().Name.ToString()+ ".Resources" + ".SelectCate.png";
      2、图片要做为项目资源参与编译如下图:

方式二、

 /// 
        /// 根据图片路径加载图片
        /// 
        /// 图片路径
        /// Bitmap格式图片
        public Bitmap GetToolIcon(string imagePathName)
        {
            Bitmap b = null;
            if (!File.Exists(imagePathName))
            {
                MessageBox.Show("该路径下:" + imagePathName.ToString() + "!文件找不到");
                return b;
            }
            try
            {
                FileStream fs = new FileStream(imagePathName, FileMode.Open, FileAccess.Read);
                b = (Bitmap)System.Drawing.Bitmap.FromStream(fs);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return b;
        }

关注
打赏
1647422595
查看更多评论
立即登录/注册

微信扫码登录

0.0594s