PreviewPure.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Metis.AutoAnalysis
  11. {
  12. public partial class PreviewPure : Form
  13. {
  14. private static PreviewPure _instacne;
  15. private static Button _switch;
  16. /// <summary>
  17. /// 启动预览
  18. /// </summary>
  19. /// <param name="swith">父窗口打开、关闭预览按钮</param>
  20. /// <returns></returns>
  21. public static PreviewPure StartPreiew(Button swith)
  22. {
  23. if (_instacne == null)
  24. {
  25. _instacne = new PreviewPure();
  26. swith.Click += Swith_Click;
  27. _instacne.FormClosing += (s, e) =>
  28. {
  29. swith.Click -= Swith_Click;
  30. swith.BackColor = SystemColors.Control;
  31. _instacne = null;
  32. };
  33. }
  34. _instacne.Show();
  35. swith.BackColor = Color.Lime;
  36. return _instacne;
  37. }
  38. private static void Swith_Click(object sender, EventArgs e)
  39. {
  40. _instacne.Close();
  41. }
  42. public PreviewPure()
  43. {
  44. InitializeComponent();
  45. }
  46. public static void ShowPrieview(Bitmap img)
  47. {
  48. if (_instacne == null)
  49. return;
  50. try
  51. {
  52. _instacne.pictureBox1.Image = (Image)img.Clone();
  53. }
  54. catch (Exception ex)
  55. {
  56. Console.WriteLine(ex.StackTrace, ex.Message);
  57. }
  58. }
  59. public static void ShowImage(string file, string name)
  60. {
  61. try
  62. {
  63. var form = new PreviewPure();
  64. form.Text = name;
  65. form.pictureBox1.Image = new Bitmap(file);
  66. form.Show();
  67. }
  68. catch
  69. { }
  70. }
  71. }
  72. }