2016-05-08 70 views
-2

我該如何去創建一個圖形實例?目前,我有這樣的:創建一個System.Drawing.Graphics的實例

public Graphics gameOverTextGFX = new Graphics; 

但我收到的錯誤: 類型「System.Drawing.Graphics」有沒有構造函數定義

爲什麼我試圖創建圖形的一個實例是原因因爲當我嘗試渲染

gameOverTextGFX.DrawString("GAME OVER!", new Font("Arial", 24), Brushes.Red, 50f, 50f); 

我收到異常:對象引用未設置爲對象的實例。

我使用C#OpenGL的,這是不是在形式,我設置大小等,爲

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

@EhsanSajjad仍然給我同樣的錯誤在我的問題 –

+0

這不會與OpenGL結合使用。在通常的Windows窗體應用程序中,您將圖形對象傳遞給'OnPaint()'方法。 –

+1

'System.Drawing.Graphics'圖形對象被認爲是普通圖像,不知道它與OpenGL有什麼關係。您通常會使用該靜態方法'Graphics.FromImage()'來獲取圖像上的圖形句柄。例如。使用'var pic =新的位圖(100,40); var graphics = Graphics.FromImage(pic);'。但是你也可以用這個控件來繪製控件,只需鉤住該控件的OnPaint()函數即可。 –

回答

1

爲了創建新的Graphics對象,需要從要繪製的控件的對象調用CreateGraphics方法。

例如,如果你想畫上一個Panel控件:

 Graphics MyGraph = panel1.CreateGraphics(); 
+1

這仍然是一條非常糟糕的路。任何繪畫都應該在具有傳遞的'Graphics'實例的'Paint'事件處理程序中進行。 – Chris

+0

@Chris我很困惑,我希望在現場寫一行文字而不是圖片 –

+1

@K。1我們仍然不知道你的設置。你已經將它標記爲OpenGL,所以使用OpenGL繪製文本而不是GDI是有意義的。你需要發佈更多的代碼。 – Chris

0

畫到System.Drawing.Bitmap,請執行下列操作:

using(Graphics g = Graphics.FromImage(bitmap)) 
{ 
    // do painting with "g" here 
} 

重要的是要處理的(或使用using blocks)所有的GDI類來防止內存泄漏。