问题描述
我正在使用Visual C#2010 Express和SDL.net图形库.
I'm working with Visual C# 2010 Express and SDL.net graphics library.
我尝试使用SurfaceControl类在winform上绘画,并且无法创建要绘画的空表面.虽然在 http://cs-sdl.sourceforge.net/apidocs/html/class_sdl_dot_net_1_1_graphics_1_1_surface.html
I try to paint on winform with SurfaceControl Class, and can't create an empty surface to draw on. I found only one working example with bitmap, although there is such method in 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未处理你调用的对象是空的.故障排除提示:使用新"关键字创建对象实例.等等.
NullReferenceException was unhandledObject reference not set to an instance of an object.Troubleshooting tips:Use the "new" keyword to create an object instance.. etc.
SDl.net有带有源代码的示例文件,它使用与我相同的方法来初始化表面变量,但是我的方法引发异常.找不到使用SurfaceControl的示例或教程.有什么想法我做错了吗?
SDl.net has example files with sources, it uses same method to initialize surface variable as i do, but mine throws exception. Can't find examples or tutorials with SurfaceControl usage. Any ideas what i am doing wrong?
还找到了本教程 http://www.microbasic.net/2011/08/using-sdl-with-c/它使用以下代码:
Also found this tutorial http://www.microbasic.net/2011/08/using-sdl-with-c/It uses this code:
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);
但是此代码还会引发相同的异常.
But this code also throws same exception.
更多信息:我设法启动了sdl.net SdlDotNetCDPlayer示例,令人惊讶的是它引发了同样的异常!尽管半年前我在笔记本电脑上工作过这些示例.
More information:I managed to launch sdl.net SdlDotNetCDPlayer example, and surprizingly it throws same exception! Though i had these examples working on my laptop half year ago.
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);
}
}
推荐答案
它可能是SdlDotNet.dll中的错误.当我引用DLL的6.1版时,我收到相同的运行时错误.当我引用DLL的5.0版本时,它运行良好.
It may be a bug in the SdlDotNet.dll. When I reference version 6.1 of the DLL, I receive the same run-time error. When I reference version 5.0 of the DLL, it runs fine.
请注意,v5对Surface使用的初始化集略有不同,因此您可能需要调整代码.
Note that v5 uses a slightly different set of initializes for the Surface, so you may need to tweak your code.
这篇关于如何在sdl.net中初始化表面变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!