• 窗体截屏(含遮挡截屏)


    1. using System;
    2. using System.Drawing;
    3. using System.Drawing.Drawing2D;
    4. using System.Drawing.Imaging;
    5. using System.IO;
    6. using System.Runtime.InteropServices;
    7. using System.Windows.Forms;
    8. class PicCompress
    9. {
    10. public byte[] GetPictureData(string imagepath)
    11. {
    12. FileStream file = new FileStream(imagepath, FileMode.Open);
    13. byte[] by = new byte[file.Length];
    14. file.Read(by, 0, by.Length);
    15. file.Close();
    16. return by;
    17. }
    18. public byte[] GetPictureData(Image imgPhoto)
    19. {
    20. //将Image转换成流数据,并保存为byte[]
    21. MemoryStream mstream = new MemoryStream();
    22. imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
    23. byte[] byData = new Byte[mstream.Length];
    24. mstream.Position = 0;
    25. mstream.Read(byData, 0, byData.Length);
    26. mstream.Close();
    27. return byData;
    28. }
    29. ///
    30. /// 无损压缩图片
    31. ///
    32. /// 原图片
    33. /// 压缩后保存位置
    34. /// 高度
    35. /// 宽度
    36. /// 压缩质量 1-100
    37. ///
    38. public bool CompressImage(Image iSource, string dFile, int dHeight, int dWidth, int flag)
    39. {
    40. if (File.Exists(dFile))
    41. {
    42. File.Delete(dFile);
    43. }
    44. ImageFormat tFormat = iSource.RawFormat;
    45. int sW = 0, sH = 0;
    46. //按比例缩放
    47. Size tem_size = new Size(iSource.Width, iSource.Height);
    48. if (tem_size.Width > dHeight || tem_size.Width > dWidth)
    49. {
    50. if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
    51. {
    52. sW = dWidth;
    53. sH = (dWidth * tem_size.Height) / tem_size.Width;
    54. }
    55. else
    56. {
    57. sH = dHeight;
    58. sW = (tem_size.Width * dHeight) / tem_size.Height;
    59. }
    60. }
    61. else
    62. {
    63. sW = tem_size.Width;
    64. sH = tem_size.Height;
    65. }
    66. Bitmap ob = new Bitmap(sW, sH);
    67. Graphics g = Graphics.FromImage(ob);
    68. g.Clear(Color.WhiteSmoke);
    69. g.CompositingQuality = CompositingQuality.HighQuality;
    70. g.SmoothingMode = SmoothingMode.HighQuality;
    71. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    72. g.DrawImage(iSource, new Rectangle(0, 0, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
    73. g.Dispose();
    74. //以下代码为保存图片时,设置压缩质量
    75. EncoderParameters ep = new EncoderParameters();
    76. long[] qy = new long[1];
    77. qy[0] = flag;//设置压缩的比例1-100
    78. EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
    79. ep.Param[0] = eParam;
    80. try
    81. {
    82. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
    83. ImageCodecInfo jpegICIinfo = null;
    84. for (int x = 0; x < arrayICI.Length; x++)
    85. {
    86. if (arrayICI[x].FormatDescription.Equals("JPEG"))
    87. {
    88. jpegICIinfo = arrayICI[x];
    89. break;
    90. }
    91. }
    92. if (jpegICIinfo != null)
    93. {
    94. ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
    95. }
    96. else
    97. {
    98. ob.Save(dFile, tFormat);
    99. }
    100. return true;
    101. }
    102. catch
    103. {
    104. return false;
    105. }
    106. finally
    107. {
    108. iSource.Dispose();
    109. ob.Dispose();
    110. }
    111. }
    112. ///
    113. /// 有遮挡截屏
    114. ///
    115. ///
    116. ///
    117. public Image CaptureScreen(Form fc)
    118. {
    119. Image MyImage = new Bitmap(fc.Width, fc.Height);
    120. Graphics g = Graphics.FromImage(MyImage);
    121. g.CopyFromScreen(fc.Location.X, fc.Location.Y, 0, 0, fc.Size);
    122. return MyImage;
    123. }
    124. ///
    125. /// 无遮挡截屏
    126. ///
    127. public Image CaptureScreen2(Form fc)
    128. {
    129. Image MyImage = null;
    130. IntPtr hdc = GetWindowDC(fc.Handle);
    131. LPPOINT lphook = new LPPOINT();
    132. lphook.x = 0;
    133. lphook.y = 0;
    134. ClientToScreen(fc.Handle, ref lphook);
    135. ScreenToClient(fc.Handle, ref lphook);
    136. Rectangle rect = new Rectangle(lphook.x, lphook.y, fc.Width, fc.Height);
    137. if (hdc != IntPtr.Zero)
    138. {
    139. IntPtr hdcMem = CreateCompatibleDC(hdc);
    140. if (hdcMem != IntPtr.Zero)
    141. {
    142. IntPtr hbitmap = CreateCompatibleBitmap(hdc, fc.Width, fc.Height);
    143. if (hbitmap != IntPtr.Zero)
    144. {
    145. IntPtr ip = SelectObject(hdcMem, hbitmap);
    146. if (ip != IntPtr.Zero)
    147. {
    148. bool a = PrintWindow(fc.Handle, hdcMem, 0);
    149. DeleteObject(hbitmap);
    150. Image tempImg = Image.FromHbitmap(hbitmap);
    151. Bitmap b = new Bitmap(tempImg);
    152. MyImage = b.Clone(rect, b.PixelFormat);
    153. }
    154. }
    155. }
    156. }
    157. return MyImage;
    158. }
    159. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    160. private static extern bool BitBlt(
    161. IntPtr hdcDest, // 目标 DC的句柄
    162. int nXDest,
    163. int nYDest,
    164. int nWidth,
    165. int nHeight,
    166. IntPtr hdcSrc, // 源DC的句柄
    167. int nXSrc,
    168. int nYSrc,
    169. System.Int32 dwRop // 光栅的处理数值
    170. );
    171. [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
    172. private static extern IntPtr GetWindowDC(
    173. IntPtr hwnd
    174. );
    175. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    176. private static extern IntPtr CreateCompatibleDC(
    177. IntPtr hdc
    178. );
    179. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    180. private static extern IntPtr CreateCompatibleBitmap(
    181. IntPtr hdc,
    182. int nWidth, // width of bitmap, in pixels
    183. int nHeight // height of bitmap, in pixels
    184. );
    185. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    186. private static extern IntPtr SelectObject(
    187. IntPtr hdc,// handle to DC
    188. IntPtr hgdiobj // handle to object
    189. );
    190. [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    191. private static extern bool DeleteObject(
    192. IntPtr hObject // handle to graphic object
    193. );
    194. [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
    195. private static extern bool PrintWindow(
    196. IntPtr hwnd, // Window to copy
    197. IntPtr hdcBlt, // HDC to print into
    198. int nFlags // Optional flags
    199. );
    200. [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
    201. private static extern int ReleaseDC(
    202. IntPtr hWnd, // handle to window
    203. IntPtr hDC // handle to DC
    204. );
    205. [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
    206. private static extern bool ScreenToClient(
    207. IntPtr hWnd, // handle to window
    208. ref LPPOINT lpPoint // screen coordinates
    209. );
    210. [System.Runtime.InteropServices.DllImportAttribute("User32.dll")]
    211. private static extern bool ClientToScreen(
    212. IntPtr hWnd, // handle to window
    213. ref LPPOINT lpPoint // screen coordinates
    214. );
    215. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    216. public struct LPPOINT
    217. {
    218. public int x;
    219. public int y;
    220. }
    221. }

    调用方法:

                PicCompress pc = new PicCompress();

                //无遮挡截屏

                Image memory = pc.CaptureScreen(this);

                //有遮挡截屏
                Image memory = pc.CaptureScreen2(this);
                //改路径
                string path = FilePath_PIC + "ABC.jpeg";

                //图像压缩
                pc.CompressImage(memory, path, this.Height, this.Width, 80);
                memory.Dispose();
                GC.Collect();

  • 相关阅读:
    (ubuntu)安装nginx
    力扣算法篇:连续子数组的最大和
    Scala继承
    【算法-双指针思想】
    同是负值像素,为何在matplotlib和opencv上显示不一样?
    文本挖掘入门
    leetcode 378. Kth Smallest Element in a Sorted Matrix(排好序的矩阵中第K小的元素)
    云计算与ai人工智能对高防cdn的发展
    JAVA mybatis操作mysql——批量增加,批量删除,多条件模糊搜索,新增时返回id和常用的增删查改
    辉芒微IO单片机FT60F010A-URT
  • 原文地址:https://blog.csdn.net/fanwenhu/article/details/126287629