简体   繁体   English

在C#中进行SlimDX屏幕捕获时,未处理NullReferenceException

[英]NullReferenceException was unhandled, when making SlimDX screen capture in C#

Trying to make a screenshot with SlimDX: 尝试使用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");
                }
            }

        }

    }
}

Getting an error: 出现错误:

NullReferenceException was unhandled. 未处理NullReferenceException。 Object reference not set to an instance of an object. 你调用的对象是空的。

at the line: 在行:

using (SlimDX.Direct3D9.Surface surface = device.GetRenderTarget(0))

What am I doing wrong? 我究竟做错了什么?

You never initialize 'device'. 您永远不会初始化“设备”。

You have public Device device; 您有public Device device; in the class definition but its never assigned. 在类定义中,但从未分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM