Search code examples
c#vb.netdrag-and-dropimgur

Visual Basic or C# drag and drop image source url


I am wanting to create and compile a small little program that allows someone to run, drag and drop an image from a browser. I then want this program to pick out the source URL for this image and paste it into a text box form. I need to do this because I am later going to be making the program upload the said URL image to Imgur at a click of a button using the API but for now I'm looking to find a way to use drag-and-drop to my advantage. I also don't know whether it would be easier to use VB.net or C#.

Can anybody give me any clues to how I may do this?

Here is what I have so far..

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 Imgur_Album_Upload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            WireDragDrop(this.Controls);
        }
        private void WireDragDrop(Control.ControlCollection ctls)
        {
            foreach (Control ctl in ctls)
            {
                ctl.AllowDrop = true;
                ctl.DragEnter += ctl_DragEnter;
                ctl.DragDrop += ctl_DragDrop;
                WireDragDrop(ctl.Controls);

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ctl_DragDrop(object sender, DragEventArgs e)
        {
            var textData = e.Data.GetData(DataFormats.Text) as string;

            if (textData == null)
                return;

            messagebox.Text = textData;
            // Validate the URL in textData here
        }
        private void ctl_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
}

Solution

  • The solution in the question you linked only seems to work with some browsers. Using "FileContents" in GetData doesn't work in Chrome for me, but works with Firefox. DataFormats.Dib would allow you to work with the bitmap directly, but unfortunately Chrome doesn't seem to support this one either.

    Specifying DataFormats.Text seems to be a reliable cross-browser solution, as it returns the image's URL with all of the browsers I tested with. DataFormats.UnicodeText might be better, but I didn't test it.

    First, set the AllowDrop property to true in whichever control you want to respond to drag-and-drop. Then, add these event handlers to the DragEnter and DragDrop events:

    private void DragEnterHandler(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Move;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    
    private void DragDropHandler(object sender, DragEventArgs e)
    {
        var textData = e.Data.GetData(DataFormats.Text) as string;
    
        if (textData == null)
            return;
    
        MessageBox.Show(textData);
        // Validate the URL in textData here
    }
    

    Visual Studio's designer can do this for you. Alternatively, you can add the handlers yourself, for example in your form's constructor:

    this.DragEnter += DragEnterHandler;
    this.DragDrop += DragDropHandler;
    // someControl.DragEnter += DragEnterHandler;
    // ...