ScreenUtil.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Drawing;
  3. using System.Windows;
  4. using System.Threading;
  5. using System.Drawing.Imaging;
  6. using System.Windows.Media.Imaging;
  7. using System.Runtime.InteropServices;
  8. using Clipboard = System.Windows.Forms.Clipboard;
  9. namespace SHJX.Service.Common.Utils
  10. {
  11. public static class ScreenUtil
  12. {
  13. public static Image CutScreen(this Rect rec)
  14. {
  15. PrintScreen();
  16. Thread.Sleep(1000);
  17. if (Clipboard.ContainsImage())
  18. {
  19. Image image = CutPicture(Clipboard.GetImage(), rec);
  20. return image;
  21. }
  22. else
  23. {
  24. Console.WriteLine("clipboard doesn't contain picture");
  25. return null;
  26. }
  27. }
  28. private static Image CutPicture(Image image, Rect rec)
  29. {
  30. PrintScreen();
  31. if (rec.X + rec.Width > image.Width)
  32. rec.Width = image.Width - rec.X;
  33. if (rec.Y + rec.Height > image.Height)
  34. rec.Height = image.Height - rec.Y;
  35. return image;
  36. }
  37. #region 系统调用
  38. private const int VK_SNAPSHOT = 0x2C;
  39. [DllImport("user32.dll")]
  40. static extern void keybd_event
  41. (
  42. byte bVk,// 虚拟键值
  43.          byte bScan,// 硬件扫描码
  44.          uint dwFlags,// 动作标识
  45.          IntPtr dwExtraInfo// 与键盘动作关联的辅加信息
  46. );
  47. private static void PrintScreen()
  48. {
  49. keybd_event((byte)VK_SNAPSHOT, 0, 0x0, IntPtr.Zero);//down  
  50. System.Windows.Forms.Application.DoEvents();//强制窗口响应按钮事件
  51. keybd_event((byte)VK_SNAPSHOT, 0, 0x2, IntPtr.Zero);//up  
  52. System.Windows.Forms.Application.DoEvents();
  53. }
  54. #endregion
  55. #region 截图转换成Bitmap
  56. /// <summary>
  57. /// 截图转换成bitmap
  58. /// </summary>
  59. /// <param name="element"></param>
  60. /// <param name="width">默认控件宽度</param>
  61. /// <param name="height">默认控件高度</param>
  62. /// <param name="x">默认0</param>
  63. /// <param name="y">默认0</param>
  64. /// <returns></returns>
  65. public static Bitmap ScreenConvertToBitmap(this System.Windows.Media.Visual visual)
  66. {
  67. int width = 0, height = 0, x = 0, y = 0;
  68. Rect rc = SystemParameters.WorkArea; //获取工作区大小
  69. width = (int)rc.Width;
  70. height = (int)rc.Height;
  71. var rtb = new RenderTargetBitmap(width, height, x, y, System.Windows.Media.PixelFormats.Default);
  72. rtb.Render(visual);
  73. Bitmap bit = BitmapSourceToBitmap(rtb);
  74. return bit;
  75. }
  76. /// <summary>
  77. /// BitmapSource转Bitmap
  78. /// </summary>
  79. /// <param name="source"></param>
  80. /// <returns></returns>
  81. public static Bitmap BitmapSourceToBitmap(BitmapSource source)
  82. {
  83. return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight);
  84. }
  85. /// <summary>
  86. /// Convert BitmapSource to Bitmap
  87. /// </summary>
  88. /// <param name="source"></param>
  89. /// <returns></returns>
  90. public static Bitmap BitmapSourceToBitmap(BitmapSource source, int width, int height)
  91. {
  92. Bitmap bmp = null;
  93. try
  94. {
  95. PixelFormat format = PixelFormat.Format24bppRgb;
  96. /*set the translate type according to the in param(source)*/
  97. switch (source.Format.ToString())
  98. {
  99. case "Rgb24":
  100. case "Bgr24": format = PixelFormat.Format24bppRgb; break;
  101. case "Bgra32": format = PixelFormat.Format32bppPArgb; break;
  102. case "Bgr32": format = PixelFormat.Format32bppRgb; break;
  103. case "Pbgra32": format = PixelFormat.Format32bppArgb; break;
  104. }
  105. bmp = new Bitmap(width, height, format);
  106. BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
  107. ImageLockMode.WriteOnly,
  108. format);
  109. source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
  110. bmp.UnlockBits(data);
  111. }
  112. catch
  113. {
  114. if (bmp != null)
  115. {
  116. bmp.Dispose();
  117. bmp = null;
  118. }
  119. }
  120. return bmp;
  121. }
  122. #endregion
  123. }
  124. }