Dani's picture

OpenTK + ASP.NET (C#)


I am new to OpenTK and programming stuff. :) I'd like to know if someone already has experience in using OpenTK and ASP.NET together. What I'm trying to do is writing an ASP.NET Web application (using C#) that renders a scene and then shows a snapshot (e.g. in gif format) in web browser. But I can't get how to write the OpenGL code so that it runs within the web application since there is no main() function there and Form in a web applications seems to be something different than Windows.Form.

Is it possible at all to use OpenGL in this context? I'll appreciate any ideas and suggestions.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

To the best of my knowledge, this hasn't been done before. You are exploring new ground here!

OpenTK requires two things to work: full trust (for platform invokes) and access to the window manager (GUI process). In other words, you should be able to host OpenTK inside an ASP.Net process iff the process runs under full trust and the server is not headless. Additionally, the process must not run as a Windows service (Vista and newer OSs do not allow services to access the window manager).

First of all, you will have to create an OpenGL context. The easiest solution is to create an empty GLControl:

using OpenTK;
using OpenTK.Graphics;
using System.Drawing;
GLControl control = new GLControl(...);

The next step is to create a framebuffer object to render into. Check out the FBO OpenGL example in Examples.exe for sample code. The idea is that you create a texture, create a FBO, attach the texture to the FBO and bind the FBO for rendering.

Once the FBO is bound, you will be able to use OpenGL normally. Call GL.Finish() once rendering is complete and readback the data from the context. The simplest solution is this (the logic is slightly more complex if you need multisampled FBOs):

Bitmap result = control.GrabScreenshot();

You can now display the resulting Bitmap or save it to disk.


  • If you need hardware acceleration, your server will actually need to have a graphics card. Intel IGPs do not fit the bill (no support for FBOs).
  • If you can get by with software acceleration, you will have to compile and use Mesa3d. Microsoft's GDI renderer won't cut it (no support for FBOs).
  • You can use OpenGL only on one thread at a time. You'll either have to serialize all OpenGL drawing or use some form of pooling to improve performance (e.g. create 10 GLControls and select a free one to work with. Use control.MakeCurrent() whenever you switch a GLControl to a new thread).
  • OpenTK in general and GLControl in particular relies on System.Windows.Forms. Can ASP.Net processes call into WinForms?

This may sound complicated, but the actual setup is less than 20 lines of code (including error checking). Feel free to post for clarifications or additional help!

szamil's picture

Hehe... My master's thesis was about this approach to the 3d visualization. :)
I've made applications based on:
Tomcat + Java servlets + JOGL,
Tomcat + Java servlets + LWJGL,
IIS + ASP.NET + Tao,
Apache (mod_mono) + Mono + Tao
and I've tested it on Windows and Linux... :) Lot of work but I have gained 100% for my thesis.

Best Regards