using System; using System.Runtime.InteropServices; namespace SharpGL { /// /// /// public class DIBSection : IDisposable { /// /// Creates the specified width. /// /// The width. /// The height. /// The bit count. /// public virtual unsafe bool Create(IntPtr hDC, int width, int height, int bitCount) { this.width = width; this.height = height; parentDC = hDC; // Destroy existing objects. Destroy(); // Create a bitmap info structure. Win32.BITMAPINFO info = new Win32.BITMAPINFO(); info.Init(); // Set the data. info.biBitCount = (short)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. hBitmap = Win32.CreateDIBSection(hDC, ref info, Win32.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0); Win32.SelectObject(hDC, hBitmap); // Set the OpenGL pixel format. SetPixelFormat(hDC, bitCount); return true; } /// /// Resizes the section. /// /// The width. /// The height. /// The bit count. public void Resize(int width, int height, int bitCount) { // Destroy existing objects. Destroy(); // Set parameters. Width = width; Height = height; // Create a bitmap info structure. Win32.BITMAPINFO info = new Win32.BITMAPINFO(); info.Init(); // Set the data. info.biBitCount = (short)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. hBitmap = Win32.CreateDIBSection(parentDC, ref info, Win32.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0); Win32.SelectObject(parentDC, hBitmap); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Destroy(); } /// /// This function sets the pixel format of the underlying bitmap. /// /// The bitcount. protected virtual bool SetPixelFormat(IntPtr hDC, int bitCount) { // Create the big lame pixel format majoo. Win32.PIXELFORMATDESCRIPTOR pixelFormat = new Win32.PIXELFORMATDESCRIPTOR(); pixelFormat.Init(); // Set the values for the pixel format. pixelFormat.nVersion = 1; pixelFormat.dwFlags = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI); pixelFormat.iPixelType = Win32.PFD_TYPE_RGBA; pixelFormat.cColorBits = (byte)bitCount; pixelFormat.cDepthBits = 32; pixelFormat.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat; if((iPixelformat = Win32.ChoosePixelFormat(hDC, pixelFormat)) == 0 ) return false; // Sets the pixel format if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return false; } return true; } /// /// Destroys this instance. /// public virtual void Destroy() { // Destroy the bitmap. if(hBitmap != IntPtr.Zero) { Win32.DeleteObject(hBitmap); hBitmap = IntPtr.Zero; } } /// /// The parent dc. /// protected IntPtr parentDC = IntPtr.Zero; /// /// The bitmap handle. /// protected IntPtr hBitmap = IntPtr.Zero; /// /// The bits. /// protected IntPtr bits = IntPtr.Zero; /// /// The width. /// protected int width = 0; /// /// The height. /// protected int height = 0; /// /// Gets the handle to the bitmap. /// /// The handle to the bitmap. public IntPtr HBitmap { get {return hBitmap;} } /// /// Gets the bits. /// public IntPtr Bits { get { return bits; } } /// /// Gets or sets the width. /// /// The width. public int Width { get { return width; } protected set { width = value; } } /// /// Gets or sets the height. /// /// The height. public int Height { get {return height;} protected set { height = value; } } } }