Advertisement
Guest User

DoubleBuffering

a guest
Jun 26th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System.Drawing;
  2. using System.Drawing.Imaging;
  3.  
  4. namespace EctoUI
  5. {
  6.     /// <summary>
  7.     /// Class to implement Double Buffering
  8.     /// NT Almond
  9.     /// 24 July 2003
  10.     /// </summary>
  11.     ///
  12.     public class DBGraphics
  13.     {
  14.         private Graphics _graphics;
  15.         private Bitmap _memoryBitmap;
  16.         private int _width;
  17.         private int _height;
  18.         private int _left;
  19.         private int _top;
  20.  
  21.         /// <summary>
  22.         /// Default constructor
  23.         /// </summary>
  24.         public DBGraphics ()
  25.         {
  26.             _width = 0;
  27.             _height = 0;
  28.             _left = 0;
  29.             _top = 0;
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Creates double buffer object
  34.         /// </summary>
  35.         /// <param name="g">Window forms Graphics Object</param>
  36.         /// <param name="width">width of paint area</param>
  37.         /// <param name="height">height of paint area</param>
  38.         /// <returns>true/false if double buffer is created</returns>
  39.         public bool CreateDoubleBuffer ( Graphics g, int width, int height, int left = 0, int top = 0 )
  40.         {
  41.             if ( _memoryBitmap != null )
  42.             {
  43.                 _memoryBitmap.Dispose();
  44.                 _memoryBitmap = null;
  45.             }
  46.  
  47.             if ( _graphics != null )
  48.             {
  49.                 _graphics.Dispose();
  50.                 _graphics = null;
  51.             }
  52.  
  53.             if ( width == 0 || height == 0 )
  54.             {
  55.                 return false;
  56.             }
  57.  
  58.             if ( ( width != _width ) || ( height != _height ) )
  59.             {
  60.                 _width = width;
  61.                 _height = height;
  62.                 _left = left;
  63.                 _top = top;
  64.  
  65.                 _memoryBitmap = new Bitmap(width, height);
  66.                 _graphics = Graphics.FromImage(_memoryBitmap);
  67.             }
  68.  
  69.             return true;
  70.         }
  71.  
  72.  
  73.         /// <summary>
  74.         /// Renders the double buffer to the screen
  75.         /// </summary>
  76.         /// <param name="gr">Window forms Graphics Object</param>
  77.         public void Render ( Graphics gr )
  78.         {
  79.             if ( _memoryBitmap != null )
  80.             {
  81.                 gr.DrawImage(_memoryBitmap, new Rectangle(_left, _top, _width, _height), _left, _top, _width, _height, GraphicsUnit.Pixel);
  82.             }
  83.         }
  84.  
  85.         /// <summary>
  86.         ///
  87.         /// </summary>
  88.         /// <returns>true if double buffering can be achieved</returns>
  89.         public bool CanDoubleBuffer ()
  90.         {
  91.             return _graphics != null;
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Accessor for memory graphics object
  96.         /// </summary>
  97.         public Graphics g
  98.         {
  99.             get
  100.             {
  101.                 return _graphics;
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement