DoubleBuffer.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 2k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using UtilityLibrary.General;
  4. namespace UtilityLibrary.WinControls
  5. {
  6. /// <summary>
  7. /// Summary description for DoubleBuffer.
  8. /// </summary>
  9. public class DoubleBuffer
  10. {
  11. #region Class Variables
  12. int bufferWidth;
  13. int bufferHeight;
  14. Bitmap surface;
  15. Graphics buffer;
  16. #endregion 
  17. #region Constructors
  18. public DoubleBuffer()
  19. {
  20. }
  21. public void Dispose() 
  22. {
  23. Cleanup();
  24. }
  25. #endregion
  26. #region Methods
  27. public Graphics RequestBuffer(int width, int height, Graphics g) 
  28. {
  29. if (width == bufferWidth && height == bufferHeight && buffer != null) 
  30. {
  31. return buffer;
  32. }
  33. Cleanup();
  34. surface = new Bitmap(width, height, g);
  35. buffer = Graphics.FromImage(surface);
  36. bufferWidth = width;
  37. bufferHeight = height;
  38. return buffer;
  39. }
  40. public void PaintBuffer(Graphics dest, int x, int y) 
  41. {
  42. dest.DrawImage(surface, x, y);
  43. }
  44. public void PaintBuffer(Graphics dest, Rectangle destRect, Rectangle sourceRect) 
  45. {
  46. dest.DrawImage(surface, destRect, sourceRect, GraphicsUnit.Pixel);
  47. }
  48. public void GDIPaintBuffer(Graphics dest, Rectangle destRect, Rectangle srcRect) 
  49. {
  50. GDIUtil.StrechBitmap(dest, destRect, srcRect, surface);
  51. }
  52. #endregion 
  53. #region Implementation
  54. private void Cleanup() 
  55. {
  56. if (buffer != null) 
  57. {
  58. buffer.Dispose();
  59. buffer = null;
  60. }
  61. if (surface != null) 
  62. {
  63. surface.Dispose();
  64. surface = null;
  65. }
  66. }
  67. #endregion
  68. }
  69. }