Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace EctoUI
- {
- /// <summary>
- /// Class to implement Double Buffering
- /// NT Almond
- /// 24 July 2003
- /// </summary>
- ///
- public class DBGraphics
- {
- private Graphics _graphics;
- private Bitmap _memoryBitmap;
- private int _width;
- private int _height;
- private int _left;
- private int _top;
- /// <summary>
- /// Default constructor
- /// </summary>
- public DBGraphics ()
- {
- _width = 0;
- _height = 0;
- _left = 0;
- _top = 0;
- }
- /// <summary>
- /// Creates double buffer object
- /// </summary>
- /// <param name="g">Window forms Graphics Object</param>
- /// <param name="width">width of paint area</param>
- /// <param name="height">height of paint area</param>
- /// <returns>true/false if double buffer is created</returns>
- public bool CreateDoubleBuffer ( Graphics g, int width, int height, int left = 0, int top = 0 )
- {
- if ( _memoryBitmap != null )
- {
- _memoryBitmap.Dispose();
- _memoryBitmap = null;
- }
- if ( _graphics != null )
- {
- _graphics.Dispose();
- _graphics = null;
- }
- if ( width == 0 || height == 0 )
- {
- return false;
- }
- if ( ( width != _width ) || ( height != _height ) )
- {
- _width = width;
- _height = height;
- _left = left;
- _top = top;
- _memoryBitmap = new Bitmap(width, height);
- _graphics = Graphics.FromImage(_memoryBitmap);
- }
- return true;
- }
- /// <summary>
- /// Renders the double buffer to the screen
- /// </summary>
- /// <param name="gr">Window forms Graphics Object</param>
- public void Render ( Graphics gr )
- {
- if ( _memoryBitmap != null )
- {
- gr.DrawImage(_memoryBitmap, new Rectangle(_left, _top, _width, _height), _left, _top, _width, _height, GraphicsUnit.Pixel);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns>true if double buffering can be achieved</returns>
- public bool CanDoubleBuffer ()
- {
- return _graphics != null;
- }
- /// <summary>
- /// Accessor for memory graphics object
- /// </summary>
- public Graphics g
- {
- get
- {
- return _graphics;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement