Draw a Bitmap Image on WinForm During Runtime


Would you like to draw a bitmap on the screen? Drawing on the WinForms is very easy using the Graphics() Class and Objects. You can draw any type of image by taking the image from your HDD and including file types like BMP, JPEG, PNG etc. Watch the screenshot of a file that is drawn to the WinForm:


The image with the Red background is drawn on a Panel. The panel acts as the Graphics() surface to which we have drawn the image. We can choose to draw on any control including the Form itself. In the case above, we have used the whole Panel to draw an unscaled image simply taken from the HDD. Press the Choose Bitmap button on the Top Right and you will be presented with the OpenFileDialog. You can then choose your image file and set it to display on the Form's Panel. See code below followed by the source code at the end: 

namespace Drawing_a_Bitmap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

Below we have created the Graphics() object with which we shall work in the program. This Graphics() object is used to create surfaces and to draw to them. We have used the Panel control to draw the Bitmap on. The first thing we need to do is give the choice to the user to choose the bitmap file through a OpenFileDialog. Then we capture the image in a Bitmap Object by supplying the FileName of the OpenFileDialog as a parameter. Then we simply create the drawing surface using the CreateGraphics() function. The final line of code draws the bitmap on the position 0,0 starting from the Top Left corner as the starting point. 

        Graphics graphics;
                
        private void btnChoose_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(openFileDialog1.FileName.ToString());
                bitmap.InitializeLifetimeService();
                
                graphics = this.TestPanel.CreateGraphics();
                graphics.DrawImage(bitmap, 0, 0);
            }
        }

The code below has one difference and that is we have supplied a fourth and fifth parameter to the DrawImage() function. This overload gives us the Custom Width in the 4th Parameter and the Custom Height in the 5th Parameter. These values are taken and converted to an Integer. Type into the variables x and y respectively from the two TextBoxes labeled Width and Height on the form then click button. 

        private void btnResizedBitmap_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(openFileDialog1.FileName.ToString());
                bitmap.InitializeLifetimeService();

                int x = Convert.ToInt16(txtWidth.Text);
                int y = Convert.ToInt16(txtHeight.Text);

                graphics = this.TestPanel.CreateGraphics();
                graphics.DrawImage(bitmap, 0, 0, x, y);
            }
        }



On pressing the Reset button, we Clear() the Graphics Object which brings a plane solid color to the drawing surface of our choice. 

        private void btnResetBitmap_Click(object sender, EventArgs e)
        {
            graphics.Clear(Color.White);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
    }
}

The graphics drawn above is not persistent i.e. when we resize or mimize the form, we may encounter a loss of a portion or the whole image. To avoid this, we must use the OnPaint() function to draw all drawings. 


Directly Download the Source Code in Visual Studio 2012