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 no 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

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 openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image files(*.jpg; *.jpeg; *.gif)|*.jpg; *.jpeg; *.gif";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Bitmap img = new Bitmap(openFileDialog.FileName);
                pictureBox1.Image = img.GetThumbnailImage(350, 350, null, IntPtr.Zero);
                openFileDialog.RestoreDirectory = true;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Image image = pictureBox1.Image;
            pictureBox2.Image = image.GetThumbnailImage(100, 100, null, IntPtr.Zero);
        }
    }
}

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

thumbnail-image


Similar Articles