C#屏幕截取、保存

private Bitmap CaptureScreen1()
        {
            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;
            System.Drawing.Rectangle bounds = GetScreenBounds();
            Bitmap bitmap=new Bitmap(bounds.Width, bounds.Height);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty,bounds.Size);

            }

            return bitmap;
        }
        private Bitmap CaptureScreen2()
        {

//处理分辨率问题
            System.Drawing.Rectangle bounds =Screen.PrimaryScreen.Bounds;

            float dpiX,dpiY;
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            { 
                dpiX =

可以通过使用SharpDX库来后台截取DX窗口。下面是一个示例代码: ``` using System; using System.Drawing; using System.Drawing.Imaging; using SharpDX; using SharpDX.Direct3D9; using SharpDX.Windows; namespace DXScreenCaptureTest { class Program { static void Main(string[] args) { var processName = "myDXProcess"; // 要截取的DX程序进程名 var adapterIndex = 0; // 显卡索引,如果只有一个显卡则为0 // 获取DX程序窗口句柄 IntPtr hWnd = IntPtr.Zero; while (true) { hWnd = User32.FindWindowEx(IntPtr.Zero, hWnd, null, processName); if (hWnd == IntPtr.Zero) break; if (User32.IsWindowVisible(hWnd)) { int pid; User32.GetWindowThreadProcessId(hWnd, out pid); if (System.Diagnostics.Process.GetProcessById(pid).ProcessName == processName) break; } } if (hWnd == IntPtr.Zero) { Console.WriteLine($"未找到进程名为{processName}的DX程序窗口"); return; } // 获取DX设备对象 var config = new PresentParameters() { BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = hWnd, PresentationInterval = PresentInterval.Immediate, SwapEffect = SwapEffect.Discard }; var device = new Device(new Direct3D(), adapterIndex, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing, config); // 获取后台缓存表面 var surface = Surface.CreateOffscreenPlain(device, config.BackBufferWidth, config.BackBufferHeight, Format.A8R8G8B8, Pool.SystemMemory); // 截取屏幕保存到文件 var bitmap = CaptureSurface(device, surface); bitmap.Save("screenshot.png", ImageFormat.Png); device.Dispose(); surface.Dispose(); bitmap.Dispose(); Console.WriteLine("截图完成"); } private static Bitmap CaptureSurface(Device device, Surface surface) { // 将后台缓存表面复制到系统内存表面 device.GetFrontBufferData(0, surface); // 将表面转换为Bitmap var dataRectangle = surface.LockRectangle(LockFlags.ReadOnly); var bitmap = new Bitmap(dataRectangle.Width, dataRectangle.Height, PixelFormat.Format32bppArgb); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Utilities.CopyMemory(bitmapData.Scan0, dataRectangle.DataPointer, dataRectangle.Pitch * dataRectangle.Height); surface.UnlockRectangle(); bitmap.UnlockBits(bitmapData); return bitmap; } } static class User32 { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); } } ``` 代码中使用了SharpDX库,需要使用NuGet进行安装。另外,要注意确保程序有足够的权限进行后台截图操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值