Исключение NullReferenceException не обрабатывалось при создании снимка экрана SlimDX в C # - PullRequest
0 голосов
/ 09 марта 2012

Попытка сделать скриншот с 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))

Что я делаю не так?

1 Ответ

3 голосов
/ 09 марта 2012

Вы никогда не инициализируете «устройство».

У вас есть public Device device; в определении класса, но он никогда не назначается.

...