public static Bitmap Capture(IntPtr Window)
        {
            IntPtr SourceDC = User32.GetWindowDC(Window),
                   MemoryDC = Gdi32.CreateCompatibleDC(SourceDC);

            var rect = new RECT();
            User32.GetWindowRect(Window, ref rect);

            int Width = rect.Right - rect.Left,
                Height = rect.Bottom - rect.Top;

            // Create a bitmap we can copy it to
            var hBmp = Gdi32.CreateCompatibleBitmap(SourceDC, Width, Height);

            try
            {
                // select the bitmap object
                var hOld = Gdi32.SelectObject(MemoryDC, hBmp);

                // bitblt over
                Gdi32.BitBlt(MemoryDC, 0, 0, Width, Height, SourceDC, 0, 0, CopyPixelOperation.SourceCopy);

                // restore selection
                Gdi32.SelectObject(MemoryDC, hOld);

                // get a .NET image object for it
                return Bitmap.FromHbitmap(hBmp);
            }
            finally { Gdi32.DeleteObject(hBmp); }
        }
Example #2
0
        public RecorderParams(string filename, int FrameRate, FourCC Encoder, int Quality,
            string AudioSourceId, bool UseStereo, bool EncodeAudio, int AudioQuality, bool IncludeCursor, IntPtr hWnd)
        {
            FileName = filename;
            FramesPerSecond = FrameRate;
            Codec = Encoder;
            this.Quality = Quality;
            this.AudioSourceId = AudioSourceId;
            this.EncodeAudio = EncodeAudio;
            AudioBitRate = Mp3AudioEncoderLame.SupportedBitRates.OrderBy(br => br).ElementAt(AudioQuality);
            this.IncludeCursor = IncludeCursor;
            this.hWnd = hWnd;
            CaptureVideo = hWnd.ToInt32() != -1;

            if (hWnd == Desktop)
            {
                System.Windows.Media.Matrix toDevice;
                using (var source = new HwndSource(new HwndSourceParameters()))
                    toDevice = source.CompositionTarget.TransformToDevice;

                Height = (int)Math.Round(SystemParameters.PrimaryScreenHeight * toDevice.M22);
                Width = (int)Math.Round(SystemParameters.PrimaryScreenWidth * toDevice.M11);
            }
            else
            {
                var rect = new RECT();
                User32.GetWindowRect(hWnd, ref rect);

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

            int val;
            IsLoopback = !int.TryParse(AudioSourceId, out val);

            WaveFormat = IsLoopback ? LoopbackDevice.AudioClient.MixFormat : new WaveFormat(44100, 16, UseStereo ? 2 : 1);
        }
Example #3
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);
        }
Example #4
0
 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
Example #5
0
 public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
Example #6
0
 public static extern int DwmGetWindowAttribute(IntPtr hWnd, DWMWindowAttribute dWAttribute, ref RECT pvAttribute, int cbAttribute);
Example #7
0
 public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref RECT margins);