• 大小: 69KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签: C#  DX  

资源简介

窗口截图C#代码 可后台截取DX窗口 使用SharpDX库 GraphicsCapture Win32.DesktopDuplication 也可以抓取桌面

资源截图

代码片段和文件信息

using System;
using System.Drawing;
using System.Drawing.Imaging;

using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;

using Win32.BitBlt.Interop;
using Win32.Shared;
using Win32.Shared.Interfaces;

using Device = SharpDX.Direct3D11.Device;
using Rectangle = System.Drawing.Rectangle;

namespace Win32.BitBlt
{
    internal class BitBlt : ICaptureMethod
    {
        private IntPtr _hWnd;

        public void Dispose()
        {
            // Nothing to do
        }

        public bool IsCapturing { get; private set; }

        public void StartCapture(IntPtr hWnd Device device Factory factory)
        {
            var picker = new WindowPicker();
            _hWnd = picker.PickCaptureTarget(hWnd);
            if (_hWnd == IntPtr.Zero)
                return;

            IsCapturing = true;
        }

        public Texture2D TryGetNextframeAsTexture2D(Device device)
        {
            if (_hWnd == IntPtr.Zero)
                return null;

            var hdcSrc = NativeMethods.GetDCEx(_hWnd IntPtr.Zero DeviceContextValues.Window | DeviceContextValues.Cache | DeviceContextValues.LockWindowUpdate);
            var hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc);
            NativeMethods.GetWindowRect(_hWnd out var rect);
            var (width height) = (rect.Right - rect.Left rect.Bottom - rect.Top);
            var hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc width height);
            var hOld = NativeMethods.Selectobject(hdcDest hBitmap);
            NativeMethods.BitBlt(hdcDest 0 0 width height hdcSrc 0 0 TernaryRasterOperations.SRCCOPY);
            NativeMethods.Selectobject(hdcDest hOld);
            NativeMethods.DeleteDC(hdcDest);
            NativeMethods.ReleaseDC(_hWnd hdcSrc);

            using var img = Image.FromHbitmap(hBitmap);
            NativeMethods.Deleteobject(hBitmap);

            using var bitmap = img.Clone(Rectangle.FromLTRB(0 0 width height) PixelFormat.Format32bppArgb);
            var bits = bitmap.LockBits(Rectangle.FromLTRB(0 0 width height) ImageLockMode.ReadOnly img.PixelFormat);

            var data = new DataBox { DataPointer = bits.Scan0 RowPitch = bits.Width * 4 SlicePitch = bits.Height };

            var texture2dDescription = new Texture2DDescription
            {
                ArraySize = 1
                BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget
                CpuAccessFlags = CpuAccessFlags.None
                Format = Format.B8G8R8A8_UNorm
                Height = height
                MipLevels = 1
                SampleDescription = new SampleDescription(1 0)
                Usage = ResourceUsage.Default
                Width = width
            };
            var texture2d = new Texture2D(device texture2dDescription new[] { data });
            bitmap.UnlockBits(bits);

            return texture2d;
        }

        public void StopCapture()
        {
            _hWnd = IntPtr.Zero;
        }
    }

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5653  2020-04-18 20:37  窗口截图(可后台截取DX窗口)\Source\CSharp窗口截图.sln

    .......       184  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\App.config

    .......      3005  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\BitBlt.cs

    .......      2872  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Interop\DeviceContextValues.cs

    .......      1460  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Interop\NativeMethods.cs

    .......       399  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Interop\RECT.cs

    .......      1711  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Interop\TernaryRasterOperations.cs

     文件       9696  2020-04-18 20:36  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\obj\Debug\Win32.BitBlt.csprojAssemblyReference.cache

    .......       520  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\packages.config

    .......       327  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Program.cs

    .......       457  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Shader.fx

    .......      4419  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.BitBlt\Win32.BitBlt.csproj

    .......       184  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\App.config

    .......      2208  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\DesktopDuplication.cs

     文件       2924  2020-04-18 20:36  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\obj\Debug\Win32.DesktopDuplication.csprojAssemblyReference.cache

    .......       284  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\packages.config

    .......       347  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\Program.cs

    .......       457  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\Shader.fx

    .......      3242  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DesktopDuplication\Win32.DesktopDuplication.csproj

    .......       184  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\App.config

    .......      2286  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\DwmSharedSurface.cs

    .......       994  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\Interop\NativeMethods.cs

     文件       8486  2020-04-18 20:36  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\obj\Debug\Win32.DwmSharedSurface.csprojAssemblyReference.cache

    .......       520  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\packages.config

    .......       347  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\Program.cs

    .......       457  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\Shader.fx

    .......      4273  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmSharedSurface\Win32.DwmSharedSurface.csproj

    .......       184  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmThumbnail\App.config

    .......       381  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmThumbnail\App.xaml

    .......       340  2019-11-18 20:04  窗口截图(可后台截取DX窗口)\Source\Win32.DwmThumbnail\App.xaml.cs

............此处省略96个文件信息

评论

共有 条评论