Sign in to follow this  
Followers 0

Rendering text using OpenTK

2 posts in this topic

In the early stages of working on a game project using Visual Studio 2012 and OpenTK. What I'm working on right now, based on a series of tutorials I found, is a way to render text reliably. The general method I'm using is to create a Bitmap, draw the text to the Bitmap, then use it as a GL texture. The current method seems pretty inefficient (I'm recreating the bitmap every frame, I could probably split it into one smaller Bitmap per string and not have to constantly redraw them), but the problem I'm having is before that. After a few seconds of generating bitmaps, the program crashes.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace FusionMan {
class TextObject {
public string text;
public PointF pos;
public TextObject(string nText, PointF nPos) {
text = nText;
pos = nPos;
class Controller {
Bitmap textBmp;
int textTexture;
Font font;
List<TextObject> textObjects;
// Called in OnLoad
public void initialize() {
// Initialize text rendering
// Create Bitmap and OpenGL texture
textBmp = new Bitmap(640, 480); // match window size
PrivateFontCollection fontCollection = new PrivateFontCollection();
font = new Font(
private void flushText() {
textObjects = new List<TextObject>();
public void addText(TextObject textObject) {
textBmp = new Bitmap(640, 480); // match window size
textTexture = GL.GenTexture();
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); // just allocate memory, so we can update efficiently using TexSubImage2D
Graphics gfx = Graphics.FromImage(textBmp);
gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
foreach (TextObject t in textObjects) {
gfx.DrawString(t.text, font, Brushes.White, t.pos);
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, 640, 480, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
private void drawText() {
GL.Ortho(0.0, 640.0, 0.0, 480.0, 0.0, 4.0);
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
GL.TexCoord2(1f, 1f); GL.Vertex2(640f, 0f);
GL.TexCoord2(1f, 0f); GL.Vertex2(640f, 480f);
GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 480f);
// Called in OnRenderFrame
public void draw() {
addText(new TextObject("Test", new PointF(10, 360)));

Error message:

System.ArgumentException was unhandled
Message=Parameter is not valid.
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at FusionMan.Controller.addText(TextObject textObject) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 50
at FusionMan.Controller.draw() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 88
at FusionMan.SimpleWindow.OnRenderFrame(FrameEventArgs e) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 61
at OpenTK.GameWindow.OnRenderFrameInternal(FrameEventArgs e)
at OpenTK.GameWindow.RaiseRenderFrame(Stopwatch render_watch, Double& next_render, FrameEventArgs render_args)
at OpenTK.GameWindow.DispatchUpdateAndRenderFrame(Object sender, EventArgs e)
at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second)
at FusionMan.SimpleWindow.Main() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 72
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
it says "Parameter is not valid", but the line in question just says

[CODE]textBmp = new Bitmap(640, 480); // match window size[/CODE]

Any idea what's going on there?

Share this post

Link to post
Share on other sites
The constructor int, int, PixelFormat throws an exception when the format does not start with Format. On the other hand according to the MSDNA the constructor int, int invokes the other one with Format32bppArgb so it shouldnt throw an ArgumentException.

You might wanna debug the bitmap constructor using the .NET source code so you can ispect where the exception is actually thrown. Here is a link how this can be done:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0