MyUtils.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace IPADDemo.Util
  10. {
  11. public static class MyUtils
  12. {
  13. public static bool IsFileInUse(string fileName)
  14. {
  15. bool inUse = true;
  16. FileStream fs = null;
  17. try
  18. {
  19. fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  20. inUse = false;
  21. }
  22. catch (Exception ex)
  23. {
  24. try
  25. {
  26. Process[] pcs = Process.GetProcesses();
  27. foreach (Process p in pcs)
  28. {
  29. if (p.MainModule.FileName == fileName)
  30. {
  31. p.Kill();
  32. }
  33. }
  34. }
  35. catch (Exception e) { }
  36. }
  37. finally
  38. {
  39. if (fs != null)
  40. fs.Close();
  41. }
  42. return inUse;//true表示正在使用,false没有使用
  43. }
  44. public static Bitmap Base64StringToImage(string basestr)
  45. {
  46. Bitmap bitmap = null;
  47. try
  48. {
  49. String inputStr = basestr;
  50. byte[] arr = Convert.FromBase64String(inputStr);
  51. MemoryStream ms = new MemoryStream(arr);
  52. Bitmap bmp = new Bitmap(ms);
  53. ms.Close();
  54. bitmap = bmp;
  55. //MessageBox.Show("转换成功!");
  56. }
  57. catch (Exception ex)
  58. {
  59. Console.WriteLine("Base64StringToImage 转换失败\nException:" + ex.Message);
  60. }
  61. return bitmap;
  62. }
  63. public static string ImageToBase64String(this Image image)
  64. {
  65. return Convert.ToBase64String(image.ImageToBytes());
  66. }
  67. }
  68. }