/HS Audio/LIBRARY/SharpGL/DIBSection.cs

https://gitlab.com/gpo04174/HSAudio · C# · 197 lines · 96 code · 30 blank · 71 comment · 6 complexity · 8d1aa0fba51475405637fd36232a9a21 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace SharpGL
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class DIBSection : IDisposable
  9. {
  10. /// <summary>
  11. /// Creates the specified width.
  12. /// </summary>
  13. /// <param name="width">The width.</param>
  14. /// <param name="height">The height.</param>
  15. /// <param name="bitCount">The bit count.</param>
  16. /// <returns></returns>
  17. public virtual unsafe bool Create(IntPtr hDC, int width, int height, int bitCount)
  18. {
  19. this.width = width;
  20. this.height = height;
  21. parentDC = hDC;
  22. // Destroy existing objects.
  23. Destroy();
  24. // Create a bitmap info structure.
  25. Win32.BITMAPINFO info = new Win32.BITMAPINFO();
  26. info.Init();
  27. // Set the data.
  28. info.biBitCount = (short)bitCount;
  29. info.biPlanes = 1;
  30. info.biWidth = width;
  31. info.biHeight = height;
  32. // Create the bitmap.
  33. hBitmap = Win32.CreateDIBSection(hDC, ref info, Win32.DIB_RGB_COLORS,
  34. out bits, IntPtr.Zero, 0);
  35. Win32.SelectObject(hDC, hBitmap);
  36. // Set the OpenGL pixel format.
  37. SetPixelFormat(hDC, bitCount);
  38. return true;
  39. }
  40. /// <summary>
  41. /// Resizes the section.
  42. /// </summary>
  43. /// <param name="width">The width.</param>
  44. /// <param name="height">The height.</param>
  45. /// <param name="bitCount">The bit count.</param>
  46. public void Resize(int width, int height, int bitCount)
  47. {
  48. // Destroy existing objects.
  49. Destroy();
  50. // Set parameters.
  51. Width = width;
  52. Height = height;
  53. // Create a bitmap info structure.
  54. Win32.BITMAPINFO info = new Win32.BITMAPINFO();
  55. info.Init();
  56. // Set the data.
  57. info.biBitCount = (short)bitCount;
  58. info.biPlanes = 1;
  59. info.biWidth = width;
  60. info.biHeight = height;
  61. // Create the bitmap.
  62. hBitmap = Win32.CreateDIBSection(parentDC, ref info, Win32.DIB_RGB_COLORS,
  63. out bits, IntPtr.Zero, 0);
  64. Win32.SelectObject(parentDC, hBitmap);
  65. }
  66. /// <summary>
  67. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  68. /// </summary>
  69. public void Dispose()
  70. {
  71. Destroy();
  72. }
  73. /// <summary>
  74. /// This function sets the pixel format of the underlying bitmap.
  75. /// </summary>
  76. /// <param name="bitCount">The bitcount.</param>
  77. protected virtual bool SetPixelFormat(IntPtr hDC, int bitCount)
  78. {
  79. // Create the big lame pixel format majoo.
  80. Win32.PIXELFORMATDESCRIPTOR pixelFormat = new Win32.PIXELFORMATDESCRIPTOR();
  81. pixelFormat.Init();
  82. // Set the values for the pixel format.
  83. pixelFormat.nVersion = 1;
  84. pixelFormat.dwFlags = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI);
  85. pixelFormat.iPixelType = Win32.PFD_TYPE_RGBA;
  86. pixelFormat.cColorBits = (byte)bitCount;
  87. pixelFormat.cDepthBits = 32;
  88. pixelFormat.iLayerType = Win32.PFD_MAIN_PLANE;
  89. // Match an appropriate pixel format
  90. int iPixelformat;
  91. if((iPixelformat = Win32.ChoosePixelFormat(hDC, pixelFormat)) == 0 )
  92. return false;
  93. // Sets the pixel format
  94. if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0)
  95. {
  96. int lastError = Marshal.GetLastWin32Error();
  97. return false;
  98. }
  99. return true;
  100. }
  101. /// <summary>
  102. /// Destroys this instance.
  103. /// </summary>
  104. public virtual void Destroy()
  105. {
  106. // Destroy the bitmap.
  107. if(hBitmap != IntPtr.Zero)
  108. {
  109. Win32.DeleteObject(hBitmap);
  110. hBitmap = IntPtr.Zero;
  111. }
  112. }
  113. /// <summary>
  114. /// The parent dc.
  115. /// </summary>
  116. protected IntPtr parentDC = IntPtr.Zero;
  117. /// <summary>
  118. /// The bitmap handle.
  119. /// </summary>
  120. protected IntPtr hBitmap = IntPtr.Zero;
  121. /// <summary>
  122. /// The bits.
  123. /// </summary>
  124. protected IntPtr bits = IntPtr.Zero;
  125. /// <summary>
  126. /// The width.
  127. /// </summary>
  128. protected int width = 0;
  129. /// <summary>
  130. /// The height.
  131. /// </summary>
  132. protected int height = 0;
  133. /// <summary>
  134. /// Gets the handle to the bitmap.
  135. /// </summary>
  136. /// <value>The handle to the bitmap.</value>
  137. public IntPtr HBitmap
  138. {
  139. get {return hBitmap;}
  140. }
  141. /// <summary>
  142. /// Gets the bits.
  143. /// </summary>
  144. public IntPtr Bits
  145. {
  146. get { return bits; }
  147. }
  148. /// <summary>
  149. /// Gets or sets the width.
  150. /// </summary>
  151. /// <value>The width.</value>
  152. public int Width
  153. {
  154. get { return width; }
  155. protected set { width = value; }
  156. }
  157. /// <summary>
  158. /// Gets or sets the height.
  159. /// </summary>
  160. /// <value>The height.</value>
  161. public int Height
  162. {
  163. get {return height;}
  164. protected set { height = value; }
  165. }
  166. }
  167. }