C# 创建System.Drawing.Graphics的实例
如何创建图形实例?我目前有:C# 创建System.Drawing.Graphics的实例,c#,opentk,C#,Opentk,如何创建图形实例?我目前有: public Graphics gameOverTextGFX = new Graphics; 但我收到了一个错误: 类型“System.Drawing.Graphics”未定义构造函数 我尝试创建图形实例的原因是,当我尝试渲染时 gameOverTextGFX.DrawString("GAME OVER!", new Font("Arial", 24), Brushes.Red, 50f, 50f); 我收到异常:对象引用未设置为对象的实例 我使用的是c#一个
public Graphics gameOverTextGFX = new Graphics;
但我收到了一个错误:
类型“System.Drawing.Graphics”未定义构造函数
我尝试创建图形实例的原因是,当我尝试渲染时
gameOverTextGFX.DrawString("GAME OVER!", new Font("Arial", 24), Brushes.Red, 50f, 50f);
我收到异常:对象引用未设置为对象的实例
我使用的是c#一个OpenGl,它不在窗体上,而是在windows中设置大小等
GameOverScene.cs-程序切换到这个场景很好,只是没有写这行
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK.Input;
using System.Drawing;
using System.Drawing.Imaging;
namespace PongGame.Scenes
{
class GameOverScene : Scene, IScene
{
public Bitmap gameOverTextBMP;
public Graphics gameOverTextGFX;
public GameOverScene(SceneManager sceneManager)
: base(sceneManager)
{
// Set the title of the window
sceneManager.Title = "Pong - Game Over";
// Set the Render and Update delegates to the Update and Render methods of this class
sceneManager.renderer = Render;
sceneManager.updater = Update;
}
public void Update(FrameEventArgs e)
{
}
public void Render(FrameEventArgs e)
{
gameOverTextGFX.DrawString("GAME OVER!", new Font("Arial", 24), Brushes.Red, 50f, 50f);
}
}
}
我被提供了一个工作的主菜单场景设置,但我已经尝试将其应用到游戏场景中,但它抛出异常,说openGL线条正在其他地方使用
public MainMenuScene(SceneManager sceneManager) : base(sceneManager)
{
// Set the title of the window
sceneManager.Title = "Pong - Main Menu";
// Set the Render and Update delegates to the Update and Render methods of this class
sceneManager.renderer = Render;
sceneManager.updater = Update;
// Create Bitmap and OpenGL texture for rendering text
textBMP = new Bitmap(sceneManager.Width, sceneManager.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // match window size
textGFX = Graphics.FromImage(textBMP);
textGFX.Clear(Color.CornflowerBlue);
textGFX.Flush();
textTexture = GL.GenTexture();
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, textTexture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textBMP.Width, textBMP.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.Disable(EnableCap.Texture2D);
}
public void Update(FrameEventArgs e)
{
}
public void Render(FrameEventArgs e)
{
GL.Viewport(0, 0, sceneManager.Width, sceneManager.Height);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, sceneManager.Width, 0, sceneManager.Height, -1, 1);
if (textBMP != null)
{
textGFX.Clear(Color.CornflowerBlue);
textGFX.DrawString("Hello", new Font("Arial", 20), Brushes.White, 0f, 0f);
textGFX.DrawString("There", new Font("Arial", 20), Brushes.Red, 0f, 25f);
// Enable the texture
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, textTexture);
BitmapData data = textBMP.LockBits(new Rectangle(0, 0, textBMP.Width, textBMP.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)textBMP.Width, (int)textBMP.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
textBMP.UnlockBits(data);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
GL.TexCoord2(1f, 1f); GL.Vertex2(sceneManager.Width, 0f);
GL.TexCoord2(1f, 0f); GL.Vertex2(sceneManager.Width, sceneManager.Height);
GL.TexCoord2(0f, 0f); GL.Vertex2(0f, sceneManager.Height);
GL.End();
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.Disable(EnableCap.Texture2D);
}
为了创建新的图形对象,需要从要绘制的控件的对象调用CreateGraphics方法 例如,如果希望在面板控件上绘制:
Graphics MyGraph = panel1.CreateGraphics();
要绘制到
系统.Drawing.Bitmap
,请执行以下操作:
using( Graphics g = Graphics.FromImage( bitmap ) )
{
// do painting with "g" here
}
处置(或使用
和块)所有GDI类以防止内存泄漏是很重要的。@EhsanSajjad在我的问题中仍然会给我同样的错误。这在与OpenGL结合使用时不起作用。在通常的Windows窗体应用程序中,将图形对象传递给OnPaint()
方法。System.Drawing.graphics
图形对象被认为是用于普通图像,不确定它与OpenGL有什么关系。您通常会使用静态方法Graphics.FromImage()
获取图像上的图形句柄。例如,使用var pic=新位图(100,40);var graphics=graphics.FromImage(pic)代码>。但是您也可以使用它在控件上绘制,只需钩住OnPaint()该控件的功能。@MaximilianGerhardt我试图使用图形在场景中渲染一行文本,但拉丝线不断抛出未设置为对象实例的对象引用
异常。当场景更改时,这是一个与以前不同的错误,请更新代码。再说一遍,什么“场景”?您似乎混淆了OpenGL和Windows窗体对象和函数。你想画什么?这仍然是一个非常糟糕的方式。任何绘制都应该在Paint
事件处理程序中使用传递的Graphics
实例进行。@Chris我感到困惑,我希望在场景中写一行文本,而不是image@K.1我们仍然不知道你的设置。您已经将其标记为OpenGL,因此使用OpenGL而不是GDI来绘制文本是有意义的。你需要发布更多的代码。