SavePic.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Runtime.InteropServices;
  6. namespace GraphicalCS
  7. {
  8. /// <summary>
  9. /// SavePic µÄժҪ˵Ã÷¡£
  10. /// </summary>
  11. public class SavePic
  12. {
  13. Image tempImage;
  14. public Image newImage
  15. {
  16. get
  17. {
  18. return tempImage;
  19. }
  20. }
  21. public SavePic()
  22. {
  23. tempImage = getPic(User32.GetDesktopWindow() );
  24. }
  25. public Image getPic(IntPtr handle)
  26. {
  27. // get te hDC of the target window
  28. IntPtr hdcSrc = User32.GetWindowDC(handle);
  29. // get the size
  30. User32.RECT windowRect = new User32.RECT();
  31. User32.GetWindowRect(handle,ref windowRect);
  32. int width = windowRect.right - windowRect.left;
  33. int height = windowRect.bottom - windowRect.top;
  34. // create a device context we can copy to
  35. IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
  36. // create a bitmap we can copy it to,
  37. // using GetDeviceCaps to get the width/height
  38. IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
  39. // select the bitmap object
  40. IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
  41. // bitblt over
  42. GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
  43. // restore selection
  44. GDI32.SelectObject(hdcDest,hOld);
  45. // clean up
  46. GDI32.DeleteDC(hdcDest);
  47. User32.ReleaseDC(handle,hdcSrc);
  48. // get a .NET image object for it
  49. Image img = Image.FromHbitmap(hBitmap);
  50. // free up the Bitmap object
  51. GDI32.DeleteObject(hBitmap);
  52. return img;
  53. }
  54. private class User32
  55. {
  56. [StructLayout(LayoutKind.Sequential)]
  57. public struct RECT
  58. {
  59. public int left;
  60. public int top;
  61. public int right;
  62. public int bottom;
  63. }
  64. [DllImport("user32.dll")]
  65. public static extern IntPtr GetDesktopWindow();
  66. [DllImport("user32.dll")]
  67. public static extern IntPtr GetWindowDC(IntPtr hWnd);
  68. [DllImport("user32.dll")]
  69. public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
  70. [DllImport("user32.dll")]
  71. public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
  72. }
  73. private class GDI32
  74. {
  75. public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
  76. [DllImport("gdi32.dll")]
  77. public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
  78. int nWidth,int nHeight,IntPtr hObjectSource,
  79. int nXSrc,int nYSrc,int dwRop);
  80. [DllImport("gdi32.dll")]
  81. public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
  82. int nHeight);
  83. [DllImport("gdi32.dll")]
  84. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  85. [DllImport("gdi32.dll")]
  86. public static extern bool DeleteDC(IntPtr hDC);
  87. [DllImport("gdi32.dll")]
  88. public static extern bool DeleteObject(IntPtr hObject);
  89. [DllImport("gdi32.dll")]
  90. public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
  91. }
  92. }
  93. }