Ejemplo n.º 1
0
        // CaptureBlt ??
        public void Screenshot(byte[] Buffer)
        {
            IntPtr hDC = User32.GetWindowDC(Params.hWnd),
                hMemDC = Gdi32.CreateCompatibleDC(hDC),
                hIcon = IntPtr.Zero;

            int CursorX = 0, CursorY = 0;

            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hDC, Params.Width, Params.Height);

            if (hBitmap != IntPtr.Zero)
            {
                IntPtr hOld = Gdi32.SelectObject(hMemDC, hBitmap);

                if (Params.hWnd != RecorderParams.Desktop)
                {
                    var rect = new RECT();
                    User32.GetWindowRect(Params.hWnd, ref rect);

                    Params.Width = rect.Right - rect.Left;
                    Params.Height = rect.Bottom - rect.Top;
                }

                Gdi32.BitBlt(hMemDC, 0, 0, Params.Width, Params.Height, hDC, 0, 0, PatBltTypes.SRCCOPY);

                Gdi32.SelectObject(hMemDC, hOld);
            }

            #region Include Cursor
            if (Params.hWnd == RecorderParams.Desktop && Params.IncludeCursor)
            {
                CursorInfo ci = new CursorInfo() { cbSize = Marshal.SizeOf(typeof(CursorInfo)) };

                IconInfo icInfo;

                if (User32.GetCursorInfo(out ci))
                {
                    if (ci.flags == User32.CURSOR_SHOWING)
                    {
                        hIcon = User32.CopyIcon(ci.hCursor);
                        if (User32.GetIconInfo(hIcon, out icInfo))
                        {
                            CursorX = ci.ptScreenPos.X - ((int)icInfo.xHotspot);
                            CursorY = ci.ptScreenPos.Y - ((int)icInfo.yHotspot);
                        }
                    }
                }
            }
            #endregion

            using (var BMP = new Bitmap(Params.Width, Params.Height))
            {
                using (var g = Graphics.FromImage(BMP))
                {
                    g.DrawImage(Bitmap.FromHbitmap(hBitmap), 0, 0);

                    if (hIcon != IntPtr.Zero)
                    {
                        Bitmap CursorBMP = Icon.FromHandle(hIcon).ToBitmap();
                        g.DrawImage(CursorBMP, CursorX, CursorY, CursorBMP.Width, CursorBMP.Height);
                    }

                    g.Flush();

                    var bits = BMP.LockBits(new Rectangle(0, 0, Params.Width, Params.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                    Marshal.Copy(bits.Scan0, Buffer, 0, Buffer.Length);
                    BMP.UnlockBits(bits);
                }
            }

            Gdi32.DeleteObject(hBitmap);
            Gdi32.DeleteDC(hMemDC);
            User32.ReleaseDC(Params.hWnd, hDC);
        }
Ejemplo n.º 2
0
 public static extern bool GetCursorInfo(out CursorInfo pci);