Display User's Clipboard Image Into Picturebox Control



Before explaining the primary subject of this article we will introduce the function which will be used in our article.

Clipboard.ContainsImage():

  • First of all it returns a Boolean type that means either "true" or "false".
  • Indicates whether there is data on the clipboard that is in the System.Windows.Forms.DataFormats.Bitmap format or that can be converted to that format.

Clipboard.GetImage():
  • It returns the image itself.
  • Retrieves the image from the clipboard.

PictureBoxSizeMode:
  • It is used to specify how an image is positioned within a System.Windows.Forms.PictureBox.
  • There are a total of five possible types of "SizeMode" for the "Picturebox" Control.

    o Normal
    o StretchImage
    o AutoSize
    o CenterImage
    o Zoom
     
  • For more understanding of the "SizeModes" see the images below:

    pictureBox1.SizeMode = PictureBoxSizeMode.Normal;

    Picture1.gif

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

    Picture2.gif

    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

    Picture3.gif

    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

    Picture4.gif

    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

    Picture5.gif


Code :

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 DisplayClipboardImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            if (Clipboard.ContainsImage())
            {

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

pictureBox1.Image = Clipboard.GetImage();
            }
            else
            {
MessageBox.Show("Clipboard is empty. Please Copy Image.");
            }
        }
    }
}