Click here to Skip to main content
15,896,207 members
Articles / Desktop Programming / Windows Forms

Image Viewer UserControl

Rate me:
Please Sign up or sign in to vote.
4.97/5 (110 votes)
12 Sep 2011CPOL10 min read 376.3K   26.7K   297  
The article is about a UserControl I wrote. Opposed to PictureBoxes and other methods of displaying images on your forms, this one provides a totally different approach.
using System.Drawing;
using System;

namespace KaiwaProjects
{
    class KP_DrawEngine
    {        	
        /// <summary>
        /// Original class to implement Double Buffering by
        /// NT Almond 
        /// 24 July 2003
        /// 
        /// Extended and adjusted by
        /// Jordy "Kaiwa" Ruiter
        /// </summary>
        /// 
		private	Graphics graphics;
		private Bitmap memoryBitmap;
		private	int	width;
		private	int	height;

        public void Dispose()
        {
            if (graphics != null)
            {
                graphics.Dispose();
            }

            if (memoryBitmap != null)
            {
                memoryBitmap.Dispose();
            }
        }

        public KP_DrawEngine()
		{
            try
            {
			    width = 0;
			    height = 0;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
            }
		}

		public bool CreateDoubleBuffer(Graphics g, int width, int height, int previewWidth, int previewHeight)
        {
            try
            {
                KaiwaProjects.KP_DrawObject.UpdatePanelsize(width, height);
                KaiwaProjects.KP_DrawObject.UpdatePreviewPanelsize(previewWidth, previewHeight);

                if (memoryBitmap != null)
                {
                    memoryBitmap.Dispose();
                    memoryBitmap = null;
                }

                if (graphics != null)
                {
                    graphics.Dispose();
                    graphics = null;
                }

                if (width == 0 || height == 0)
                    return false;

                this.width = width;
                this.height = height;

                memoryBitmap = new Bitmap(width, height);
                graphics = Graphics.FromImage(memoryBitmap);

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
                return false;
            }
		}

		public void Render(Graphics g)
		{
            try
            {
                if (memoryBitmap != null)
                {
                    g.DrawImage(memoryBitmap, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
            }
		}

		public bool CanDoubleBuffer()
		{
            try
            {
                return graphics != null;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
                return false;
            }
		}

		public Graphics g 
		{
			get 
			{ 
				return graphics; 
			}
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Kaiwa-Projects
Netherlands Netherlands
Currently a student in Application Development. Is currently working towards the Game-Development business to program 3D applications.

Comments and Discussions