星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageHelper.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// 图片帮助类
  11. /// 创建人:赵耀
  12. /// 创建时间:2018年11月1日
  13. /// </summary>
  14. public class ImageHelper
  15. {
  16. /// <summary>
  17. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  18. /// </summary>
  19. /// <param name="path">图片位置</param>
  20. /// <returns></returns>
  21. public static Bitmap ReadBitmapFile(string path)
  22. {
  23. try
  24. {
  25. if (File.Exists(path))
  26. {
  27. FileStream fs = File.OpenRead(path); //OpenRead
  28. int filelength = 0;
  29. filelength = (int)fs.Length; //获得文件长度
  30. Byte[] image = new Byte[filelength]; //建立一个字节数组
  31. fs.Read(image, 0, filelength); //按字节流读取
  32. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  33. fs.Close();
  34. Bitmap bit = new Bitmap(result);
  35. return bit;
  36. }
  37. else
  38. return null;
  39. }
  40. catch (Exception)
  41. {
  42. return null;
  43. }
  44. }
  45. /// <summary>
  46. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  47. /// </summary>
  48. /// <param name="path">图片位置</param>
  49. /// <returns></returns>
  50. public static Image ReadImageFile(string path)
  51. {
  52. try
  53. {
  54. if (File.Exists(path))
  55. {
  56. FileStream fs = File.OpenRead(path); //OpenRead
  57. int filelength = 0;
  58. filelength = (int)fs.Length; //获得文件长度
  59. Byte[] image = new Byte[filelength]; //建立一个字节数组
  60. fs.Read(image, 0, filelength); //按字节流读取
  61. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  62. fs.Close();
  63. return result;
  64. }
  65. else
  66. return null;
  67. }
  68. catch (Exception)
  69. {
  70. return null;
  71. }
  72. }
  73. }
  74. }