Пример #1
0
 static ScreenGrab Grab(int left, int top, int width, int height)
 {
     using (System.Drawing.Bitmap screenBmp = BitBltGrab(left, top, width, height))
     {
         IntPtr       hBitmap = screenBmp.GetHbitmap();
         BitmapSource image   = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
         Win.DeleteObject(hBitmap);
         return(new ScreenGrab()
         {
             Image = image, Time = DateTime.Now, TopLeft = new Point(left, top)
         });
     }
 }
Пример #2
0
        /// <summary>
        /// Grabs the specified area of the screen including alpha blended windows on top such as tool tips and menus.
        /// IMPORTANT: You must call dispose on the bitmap returned by this function as soon as you are done with it.
        /// </summary>
        private static System.Drawing.Bitmap BitBltGrab(int left, int top, int width, int height)
        {
            IntPtr hDesktop      = Win.GetDesktopWindow();
            IntPtr dcDesktop     = Win.GetWindowDC(hDesktop);
            IntPtr dcTarget      = Win.CreateCompatibleDC(dcDesktop);
            IntPtr hTargetBitmap = Win.CreateCompatibleBitmap(dcDesktop, width, height);
            IntPtr hOldBitmap    = Win.SelectObject(dcTarget, hTargetBitmap);
            bool   success       = Win.BitBlt(dcTarget, 0, 0, width, height, dcDesktop, left, top, System.Drawing.CopyPixelOperation.SourceCopy | System.Drawing.CopyPixelOperation.CaptureBlt);

            System.Drawing.Bitmap bitmap = System.Drawing.Bitmap.FromHbitmap(hTargetBitmap);
            Win.SelectObject(dcTarget, hOldBitmap);
            Win.DeleteObject(hTargetBitmap);
            Win.DeleteDC(dcTarget);
            Win.ReleaseDC(hDesktop, dcDesktop);
            return(bitmap);
        }