Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#
Article

Drop images from IE

Rate me:
Please Sign up or sign in to vote.
4.50/5 (19 votes)
9 Jun 20072 min read 28.7K   347   30   2
Capturing the URL of an image dropped on your form from IE

Introduction

In a recent project I found the need to be able to drop an image from Internet Explorer onto my form and capture the URL of the image. If you have tried using any of the text formats to get the drop data then you know this does not work. At best you will get the locally cached path of the image file. There is little or no documentation on the subject, and attempting to search for information only yields others seeking the same answers. This is why I decided to make this an article. Note that this code makes use of regular expressions. As this is not an article about regex's, which is a huge topic in itself, I provide no explanation of the expressions used. You can easily find numerous tutorials on Regular Expressions.

Background

The key to reading the source URL of the image is in the data type. Through much trial and error, I found that the magic words are "HTML Format", as you will see below.

Using the code

Simply place the appropriate code below into your DragEnter and DragDrop events, add the required functions and that's all.

The DragEnter event would look as such:

C#
private void myControl_DragEnter(object sender, DragEventArgs e)
{
  if (e.Data.GetDataPresent("HTML Format"))
    e.Effect = DragDropEffects.Copy;
}

To actually capture the URL, place the following in the DragDrop event:

C#
private void myControl_DragDrop(object sender, DragEventArgs e)
{
  string clipboardHtml = (string)e.Data.GetData("HTML Format");
  string htmlFragment = getHtmlFragment(clipboardHtml);
  string imageSrc = parseImageSrc(htmlFragment);
  string baseURL = parseBaseURL(clipboardHtml);

  if (imageSrc.ToUpper().IndexOf("HTTP://") == 0)
    MessageBox.Show(imageSrc);
  else
    MessageBox.Show(baseURL + imageSrc.Substring(1));
}

We need the if statement in the above because the base URL may or may not be included in the image tag.

There are three supporting functions:

  • getHtmlFragment: Retrieves the complete image tag, including width, alt text, etc.
  • parseImageSrc: Retrieves the path of the image itself. The path is only relative so the next function is needed still.
  • parseBaseURL: Retrieves the base URL as the name implies.

C#
string getHtmlFragment(string clipboardHtml)
{
  int fragStartPos = int.Parse(Regex.Match(clipboardHtml, 
    @"^StartFragment:(\d+)", RegexOptions.Multiline).Groups[1].Value);
  int fragEndPos = int.Parse(Regex.Match(clipboardHtml, 
    @"^EndFragment:(\d+)", RegexOptions.Multiline).Groups[1].Value);
  return clipboardHtml.Substring(fragStartPos, fragEndPos - fragStartPos);
}

string parseImageSrc(string html)
{
  return Regex.Match(html, @"<img.*?src=[""'](.*?)[""'].*>", 
    RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
}

string parseBaseURL(string html)
{
  return Regex.Match(html, @http://.*?/, 
        RegexOptions.IgnoreCase).Groups[0].Value;
}

Points of Interest

Check out the demo project to see it in action.

History

  • Original posting - June 10, 2007

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat job! But sometimes won't work. Pin
ls2514520-Nov-13 15:42
ls2514520-Nov-13 15:42 
GeneralIf you liked the article please vote! Pin
Jim Weiler10-Jun-07 6:44
Jim Weiler10-Jun-07 6:44 
Thanks!

@Jim

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.