Sunday, June 18, 2017

Copy Paste Images Directly into Widows Form C#

 Hi in this tutorial we are going to see "how to set image in a clipboard directly as a Windows form Background". Lets Create a new Windows Form Project and add a following code in it.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CopyPasteImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int id = 0;
            MakeContextMenu();
        }

        private void MakeContextMenu()
        {
            ContextMenuStrip menuStrip = new ContextMenuStrip();
            ToolStripMenuItem menu = new ToolStripMenuItem("Paste");

            menu.Click += new EventHandler(menu_Click);
            menu.Name = "Paste";
            menuStrip.Items.Add(menu);

            this.ContextMenuStrip = menuStrip;
        }//End


        void menu_Click(object sender,EventArgs e)
        {
            ToolStripItem menuItem = (ToolStripItem)sender;
            if(menuItem.Name == "Paste")
            {
                PasteImage();
            }
        }//End

        private void PasteImage()
        {
            try
            {
                if (Clipboard.ContainsImage())
                {
                    Bitmap bmp = Clipboard.GetImage() as Bitmap;
                    this.BackgroundImage = bmp;                
                }
                else
                {
                    MessageBox.Show("No Image found in clipboard", "Information Message",MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }//End
    }
}

Output ScreenShot



Download Source Code


No comments:

Post a Comment