none
Copy/Paste an image on to a user control/canvas (C# winforms) RRS feed

  • Question

  • Hello All,

    I am having a desktop application (C#, VS 2017) built with .NET 4.6.1.

    it looks almost like a MS word or MS paint application where we have a white canvas area where you could draw graphical shapes using mouse. when we drag the mouse it picks the co-ordinates and draw a rectangle or a square. you can also copy/paste these graphical objects or text and paste to the same canvas.

    My new requirement is to

    1. Paste an image to the same canvas from the clipboard (exactly similar to what we do with MS word). 

    2. Insert an image from a file (exactly similar to what we do with MS word insert picture)

    Please help to proceed with this. Thanks.


    • Edited by BKoshy Tuesday, January 14, 2020 12:26 PM
    Tuesday, January 14, 2020 12:13 PM

All replies

  • Hi BKoshy,

    Thank you for posting here.

    Richtextbox seems to fulfill your requirements.

    Here is the code.

            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;
                dialog.Title = "Open folder";
                dialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string path = dialog.FileName;
                    Bitmap bmp = new Bitmap(path);
                    Clipboard.SetDataObject(bmp);
                    DataFormats.Format dataFormat =
                    DataFormats.GetFormat(DataFormats.Bitmap);
                    if (richTextBox1.CanPaste(dataFormat))
                        richTextBox1.Paste(dataFormat);
                }
    
            }

    There is a better way, we can add two events to drag and drop images directly into Richtextbox.

            public Form1()
            {
                InitializeComponent();
                richTextBox1.AllowDrop = true;
                richTextBox1.DragDrop += RichTextBox1_DragDrop;
                richTextBox1.DragEnter += RichTextBox1_DragEnter;
            }
    
            private void RichTextBox1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    e.Effect = DragDropEffects.Link;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }
    
    
            private void RichTextBox1_DragDrop(object sender, DragEventArgs e)
            {
                string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                //richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
                Bitmap bmp = new Bitmap(path);
                Clipboard.SetDataObject(bmp);
                DataFormats.Format dataFormat =
                DataFormats.GetFormat(DataFormats.Bitmap);
                if (richTextBox1.CanPaste(dataFormat))
                    richTextBox1.Paste(dataFormat);
            }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 15, 2020 8:59 AM
  • Hi BKoshy,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 7, 2020 6:58 AM