3

我不明白为什么一个变量初始化正常而另一个为空,尽管这里描述了这两种方法

表面(System.Drawing.Bitmap 位图)

从 System.Drawing.Bitmap 对象创建一个 SdlImage 实例。从 System.Drawing.Bitmap 对象加载位图,通常从资源中获取。

表面(整数宽度,整数高度)

创建给定宽度和高度的表面。

代码示例:

        Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png")); //this works {SdlDotNet.Graphics.Surface}
        surfaceControl1.Blit(surf, new Point(0, 0));
        surfaceControl1.Blit(surf, new Point(20, 20));
        
        Surface surf2 = new Surface(20, 20); //this is {null} and throws exception
        surf2.Fill(Color.White);
        surfaceControl1.Blit(surf2);

完整代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void surfaceControl1_Click(object sender, EventArgs e)
    {
        Surface surf = new Surface((Bitmap)Bitmap.FromFile("example.png"));
        surfaceControl1.Blit(surf, new Point(0, 0));
        surfaceControl1.Blit(surf, new Point(20, 20));
        //this works

        Surface surf2 = new Surface(20, 20); //<-this throws null exception, details below
        surf2.Fill(Color.White);
        surfaceControl1.Blit(surf2);
    }
}

堆栈跟踪:program.main 上的第 18 行是 Application.Run(new Form1());

sdl_control_surfaces_test.exe!WindowsFormsApplication1.Form1.surfaceControl1_Click(object sender, System.EventArgs e) Line 34   
sdl_control_surfaces_test.exe!WindowsFormsApplication1.Program.Main() Line 18 + 0x1d bytes

错误:

NullReferenceException 是未处理
的对象引用未设置为对象的实例。
疑难解答提示:
使用“new”关键字创建对象实例..等。

我发现了一个更详细的堆栈跟踪:

   at SdlDotNet.Graphics.VideoInfo.get_VideoInfoStruct()
   at SdlDotNet.Graphics.VideoInfo.get_PixelFormat()
   at SdlDotNet.Graphics.Surface..ctor(Int32 width, Int32 height)
   at WindowsFormsApplication1.Form1.surfaceControl1_Click(Object sender, EventArgs e) in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Form1.cs:line 34
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WindowsFormsApplication1.Program.Main() in C:\Users\Saska\documents\visual studio 2010\Projects\sdl_control_surfaces_test\sdl_control_surfaces_test\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

在此之前我在这里创建了另一个问题,我研究了一些我发现很少的教程和示例,它们也抛出了同样的异常,晚上我会打开我的旧笔记本电脑并检查这些示例是否可以在那里工作,因为我发誓他们做到了。

4

1 回答 1

1

该库是开源的,因此您可以前往 Sourceforge 并查找代码。

http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/Sur​​face.cs

在第 204 行中,您看到对您而言失败的构造函数只是在调用另一个

public Surface(int width, int height) : this(width, height, VideoInfo.BitsPerPixel){ }

- 如果你去http://sourceforge.net/p/cs-sdl/code/HEAD/tree/trunk/SdlDotNet/src/Graphics/VideoInfo.cs 你可以看到 BitsPerPixel 是 PixelFormat.BitsPerPixel 和PixelFormat 是一些讨厌的东西:

private static Sdl.SDL_PixelFormat PixelFormat
{
    get
    {
        return (Sdl.SDL_PixelFormat)
            Marshal.PtrToStructure(VideoInfoStruct.vfmt,
            typeof(Sdl.SDL_PixelFormat));
    }
}

VideFormat 有一个“IsInitialized”属性。所以我的猜测是你需要以某种方式初始化 VideoInfo 类。

于 2014-07-03T14:13:37.867 回答