Exemple #1
0
        private static Bitmap CaptureScreenRegion(Rectangle crop)
        {
            Rectangle totalSize = Rectangle.Empty;

            foreach (Screen s in Screen.AllScreens)
            {
                totalSize = Rectangle.Union(totalSize, s.Bounds);
            }

            IntPtr hSrc  = WindowsApi.CreateDC("DISPLAY", null, null, 0);
            IntPtr hDest = WindowsApi.CreateCompatibleDC(hSrc);
            IntPtr hBmp  = WindowsApi.CreateCompatibleBitmap(hSrc,
                                                             crop.Right -
                                                             crop.Left,
                                                             crop.Bottom -
                                                             crop.Top);
            IntPtr hOldBmp = WindowsApi.SelectObject(hDest, hBmp);

            WindowsApi.BitBlt(hDest, 0, 0, crop.Right - crop.Left,
                              crop.Bottom - crop.Top, hSrc, crop.Left, crop.Top,
                              CopyPixelOperation.SourceCopy |
                              CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Image.FromHbitmap(hBmp);

            WindowsApi.SelectObject(hDest, hOldBmp);
            WindowsApi.DeleteObject(hBmp);
            WindowsApi.DeleteDC(hDest);
            WindowsApi.DeleteDC(hSrc);

            return(bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height),
                             PixelFormat.Format24bppRgb));
        }
Exemple #2
0
 private void OnPaint(object sender, PaintEventArgs e)
 {
     if (_dwmComposited)
     {
         var rc = new Rectangle(0, ClientSize.Height - 35,
                                ClientSize.Width, 35);
         IntPtr destdc    = e.Graphics.GetHdc();
         IntPtr memdc     = WindowsApi.CreateCompatibleDC(destdc);
         IntPtr bitmapOld = IntPtr.Zero;
         var    dib       = new BitmapInfo {
             biHeight      = -(rc.Bottom - rc.Top),
             biWidth       = rc.Right - rc.Left,
             biPlanes      = 1,
             biSize        = Marshal.SizeOf(typeof(BitmapInfo)),
             biBitCount    = 32,
             biCompression = 0
         };
         if (WindowsApi.SaveDC(memdc) != 0)
         {
             IntPtr tmp;
             IntPtr bitmap = WindowsApi.CreateDIBSection(memdc, ref dib,
                                                         0, out tmp,
                                                         IntPtr.Zero, 0);
             if (!(bitmap == IntPtr.Zero))
             {
                 bitmapOld = WindowsApi.SelectObject(memdc, bitmap);
                 WindowsApi.BitBlt(destdc, rc.Left, rc.Top,
                                   rc.Right - rc.Left, rc.Bottom - rc.Top,
                                   memdc, 0, 0,
                                   CopyPixelOperation.SourceCopy);
             }
             WindowsApi.SelectObject(memdc, bitmapOld);
             WindowsApi.DeleteObject(bitmap);
             WindowsApi.DeleteDC(memdc);
         }
         e.Graphics.ReleaseHdc(destdc);
     }
     e.Graphics.DrawImage(_ssButtonImage,
                          new Rectangle(ssButton.Location, ssButton.Size));
 }