Created
December 1, 2016 01:17
-
-
Save WernerWenz/67d6f3ebd0309498ed89bcff6c1889a7 to your computer and use it in GitHub Desktop.
OpenTK Render2Bitmap sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing; | |
using System.Drawing.Imaging; | |
using OpenTK.Graphics; | |
using OpenTK.Graphics.OpenGL; | |
using SDPixelFormat = System.Drawing.Imaging.PixelFormat; | |
using PixelFormat = OpenTK.Graphics.OpenGL.PixelFormat; | |
namespace RenderToBmp | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
var mode = new GraphicsMode(new ColorFormat(8, 8, 8, 8), 24, 0, 0, ColorFormat.Empty, 1); | |
var win = new OpenTK.GameWindow(640, 480, mode, "", OpenTK.GameWindowFlags.Default, OpenTK.DisplayDevice.Default, 3, 0, GraphicsContextFlags.Default); | |
win.Visible = false; | |
win.MakeCurrent(); | |
/* START OF YOUR ACTUAL GL CODE */ | |
GL.ClearColor(0.7f, 0.7f, 1.0f, 1.0f); | |
GL.Clear(ClearBufferMask.ColorBufferBit); | |
/* END OF YOUR ACTUAL GL CODE */ | |
GL.Flush(); | |
using (var bmp = new Bitmap(640, 480, SDPixelFormat.Format32bppArgb)) | |
{ | |
var mem = bmp.LockBits(new Rectangle(0, 0, 640, 480), ImageLockMode.WriteOnly, SDPixelFormat.Format32bppArgb); | |
GL.PixelStore(PixelStoreParameter.PackRowLength, mem.Stride / 4); | |
GL.ReadPixels(0, 0, 640, 480, PixelFormat.Bgra, PixelType.UnsignedByte, mem.Scan0); | |
bmp.UnlockBits(mem); | |
bmp.Save(@"D:\test.png", ImageFormat.Png); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment