Question

Trying to make a screenshot with 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. 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?

Was it helpful?

Solution

You never initialize 'device'.

You havepublic Device device; in the class definition but its never assigned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top