我如何创建图形实例?我目前有这个:
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);
}
答案 0 :(得分:1)
为了制作新的Graphics对象,你需要从你想要绘制的控件的对象中调用CreateGraphics方法。
例如,如果您希望在Panel控件上绘图:
Graphics MyGraph = panel1.CreateGraphics();
答案 1 :(得分:0)
要绘制到System.Drawing.Bitmap
,请执行以下操作:
using( Graphics g = Graphics.FromImage( bitmap ) )
{
// do painting with "g" here
}
处理(或使用using
块)所有GDI类以防止内存泄漏非常重要。