关键词搜索

源码搜索 ×
×

WinForm 加载本地图片

发布2014-12-24浏览6717次

详情内容

方式一:

  1. /// <summary>
  2. /// 从程序集中加载图片
  3. /// </summary>
  4. /// <param name="imagePathName">图片路径(该图片必须是嵌入的资源)</param>
  5. /// <returns>Bitmap 格式图片</returns>
  6. public Bitmap GetIcon(string imagePathName)
  7. {
  8. Bitmap b = null;
  9. try
  10. {
  11. Assembly asm1 = Assembly.GetExecutingAssembly();
  12. string name = asm1.GetName().Name;
  13. asm1.GetManifestResourceStream(imagePathName);
  14. Stream imageStream = asm1.GetManifestResourceStream(imagePathName);
  15. if (imageStream != null)
  16. {
  17. b = new Bitmap(Image.FromStream(imageStream));
  18. b.MakeTransparent();
  19. }
  20. }
  21. catch (Exception e)
  22. {
  23. throw new Exception(e.Message);
  24. }
  25. return b;
  26. }

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


方式二、

  1. /// <summary>
  2. /// 根据图片路径加载图片
  3. /// </summary>
  4. /// <param name="imagePathName">图片路径</param>
  5. /// <returns>Bitmap格式图片</returns>
  6. public Bitmap GetToolIcon(string imagePathName)
  7. {
  8. Bitmap b = null;
  9. if (!File.Exists(imagePathName))
  10. {
  11. MessageBox.Show("该路径下:" + imagePathName.ToString() + "!文件找不到");
  12. return b;
  13. }
  14. try
  15. {
  16. FileStream fs = new FileStream(imagePathName, FileMode.Open, FileAccess.Read);
  17. b = (Bitmap)System.Drawing.Bitmap.FromStream(fs);
  18. }
  19. catch (Exception e)
  20. {
  21. throw new Exception(e.Message);
  22. }
  23. return b;
  24. }


相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载