0
使SlimDX屏幕捕獲時,試圖讓一個屏幕截圖與SlimDX:的NullReferenceException是未處理的,在C#
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))
我在做什麼錯?
雖然我假設在構造函數中調用InitializeComponent()來初始化'device',但這是一個很好的觀點。 – 2012-03-08 21:09:56
我試圖移動「設備」上面和下面的InitializeComponent(),如在不同的計算器問題中提出的,但它什麼也沒做。 – Alex 2012-03-08 21:13:41
我不認爲它會。設備在這種情況下由用戶明確定義,因此您可以期望初始化遵循... – MoonKnight 2012-03-08 21:15:48