在C#中进行SlimDX屏幕捕获时,未处理NullReferenceException
本文关键字:未处理 NullReferenceException 屏幕 SlimDX | 更新日期: 2023-09-27 18:27:16
尝试使用SlimDX:进行屏幕截图
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SlimDX.Direct3D9;
using SlimDX;
namespace dxcapture
{
public partial class Form1 : Form
{
public Device device;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SlimDX.Direct3D9.Surface surface = device.GetRenderTarget(0))
{
using (SlimDX.Direct3D9.Surface surface2 = SlimDX.Direct3D9.Surface.CreateOffscreenPlain(device, surface.Description.Width, surface.Description.Height, surface.Description.Format, SlimDX.Direct3D9.Pool.SystemMemory))
{
device.GetRenderTargetData(surface, surface2);
Bitmap bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(surface2, SlimDX.Direct3D9.ImageFileFormat.Bmp, new Rectangle(0, 0, 110, 110)));
bitmap.Save(@"c:'wqwqwqwqwqwqwqwq.bmp");
}
}
}
}
}
获取错误:
未处理NullReferenceException。对象引用未设置为对象的实例。
在线路上:
using (SlimDX.Direct3D9.Surface surface = device.GetRenderTarget(0))
我做错了什么?
您永远不会初始化"设备"。
在类定义中有public Device device;
,但从未分配它。