Upload Image and Create Thumbnail in C# 4.0

Introduction

In this article you will learn how to upload an image in a Windows Froms application because there is not a file upload control available in Windows Froms applications like for web applications. So in this article I will upload a picture manually using C# code and display the picture in a picturebox control and will also create a thumbnail of the image in a very simple way. A thumbnail image is a reduced copy of the image. It is very small in size. So uploading a reduced image is very easy because it is reduced in size, which is a major advantage of a thumbnail image.

Use the following procedure to create it.

First of all insert two picture boxes and two button controls from the toolbox of the Windows Forms form.

design-window-for-upload-picture.jpg

Now write the following simple C# code in the from1.cs page.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace browse_image

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog opendialog = new OpenFileDialog();

            opendialog .Filter ="Image files(*.jpg; *.jpeg; *.gif)| *.jpg; *.jpeg; *.gif";

            if (opendialog.ShowDialog() == DialogResult.OK)

            {

                Bitmap img = new Bitmap (opendialog .FileName);

                pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());

                opendialog.RestoreDirectory = true;

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Image imag = pictureBox1.Image;

            pictureBox2.Image = imag.GetThumbnailImage(100, 100, nullnew IntPtr() );

        }

    }

}

 

 Now run your application and upload a picture by clicking on the "Upload picture" button. After uploading the picture in picturebox1 then click on the "Thumbnail image" button to see the reduced image copy of the uploaded picture in picturebox1.

thumbnail-image.jpg