我正在使用 Visual C# 2010 Express 和 SDL.net 图形库。
我尝试使用 SurfaceControl 类在 winform 上绘画,但无法创建一个空表面来绘制。我发现只有一个使用位图的工作示例,尽管在http://cs-sdl.sourceforge.net/apidocs/html/class_sdl_dot_net_1_1_graphics_1_1_surface.html中有这样的方法
Surface (int width, int height) // "Create surface of a given width and height."`
我的代码:
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); // <- throws exception on click
surf2.Fill(Color.White);
surfaceControl1.Blit(surf2);
}
也试过:
Surface surf2 = new Surface(this.surfaceControl1.Width,this.surfaceControl1.Height);
NullReferenceException 是未处理的对象引用未设置为对象的实例。疑难解答提示:使用“new”关键字创建对象实例..等。
SDl.net 有带有源的示例文件,它使用与我相同的方法来初始化表面变量,但我的抛出异常。找不到使用 SurfaceControl 的示例或教程。任何想法我做错了什么?
还找到了本教程http://www.microbasic.net/2011/08/using-sdl-with-c/ 它使用以下代码:
Surface surface = new Surface(100, 100); //same error here
Surface item = new Surface((Bitmap)Bitmap.FromFile(“example.png”));
surface.Blit(item, new Point(0, 0));
surface.Blit(item, new Point(20, 20));
this.surfaceControl.Blit(surface);
但是这段代码也抛出了同样的异常。
更多信息:我设法启动了 sdl.net SdlDotNetCDPlayer 示例,令人惊讶的是它抛出了同样的异常!虽然半年前我有这些例子在我的笔记本电脑上工作。
protected override void OnResize(EventArgs e)
{
try
{
surf =
new Surface(
this.surfaceControl.Width,
this.surfaceControl.Height); //exception error
base.OnResize(e);
}
catch (AccessViolationException ex)
{
Console.WriteLine(ex.StackTrace);
}
}