Getting Image Source URL

Hey everybody,
I'm trying to drag-and-drop an image from a web page to my application. What I wanna do is to get the image source url.. When I drag an image to my app. I got the image itself or the target link (if the image is a link HREF)..
How can I get the source url of the image.. For example, in the upper-left side of this forums there is a "msdnR" image.. When I drag and drop it to my app, I got as a result the image itself and the target of the image (http://msdn.microsoft.com).. What I want from my app to do, is to give me the location of the image itself, in this example, I wanna get http://forums.microsoft.com/library/toolbar/3.0/images/banners/msdn_masthead_ltr.gif as a result..
Can you help me on this case
Thanks ahead,



Answer this question

Getting Image Source URL

  • Wee Bubba

    The browser only drops an image object on your application, it doesn't drop a link; so, no, there's nothing you can do to get it to do something else.

  • Paul Mitchell

    You can add a WebBrowser control to your form and set "AllowWebBrowserDrop" to "True" (default value). Then you can handle the DocumentComplete event of the WebBrowser:

    MessageBox.Show(e.Url.ToString());

    This way you get both picture display and the Url to it.

    I tried to drag Google logo from Google homepage and it worked fine. However when I tried the msdn logo on this site I got the whole site. To avoid user dragging content other than pictures, you can handle the Navigating event of WebBrowser and do something like:

    if (!e.Url.ToString().EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))

    e.Cancel = true;

    Above code check if the URL ends with a .jpg and only allow to navigate to such Uri. You can extend this to support other image types.

    You can drag the picture itself, or a link to the picture, both case will work fine.



  • Marimuthu_r

    But the image isn't always a hyperlink.. I can be just an Image in the web page, so it's not a hyperlink, and when I drag it to my app I get nothing..

    About hyperlinks, I can drag an image to my app, and by using the WebRequest and WebResponse I can figure out its URL, but here I will need to open the target, and to search the HTML code of the page to look for the image url in the code.. But in the cases where the images isn't a link, I can't get it's url (I know I copy the page url and I can take the url of the image from its HTML code).. But is there a way I can just drag and drop the image in my app and as a result I will get the source of the image As in dragging files in the hard drive, we can know the souce of the file.. but what's if the file is a web image There must be a way to get this data..

    do you have any idea



  • Daniz

    I saved the project. The code below is working fine for me. Be aware that my code only accept "jpg" files. Did you drag a different type of image

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace DragBrowserPicture

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

    MessageBox.Show(e.Url.ToString());

    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

    {

    if (!e.Url.ToString().EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))

    e.Cancel= true;

    }

    }

    }



  • Blair Allen Stark

    If the browser is dropping an image object there's not much you can do. The source has to drop the type of object you want to accept, which it doesn't.

    Have you tried drag-drop of a hyperlink to see that it drops URLs that you can accept

  • brian_j

    For debugging purpose, output the e.Url in Navigating handler to see what the Url looks like. This may give you some idea what went wrong

  • Martin2233

    first of all thanks for ur replays.. So, I did what'd you wrote above, what ur process do, it downloads the image to my hard drive, and it give me the location on the file in my hard drive.. Okay, using this idea I can generate the file name, but what's if the image is a URL here I stuck again..
    On the other hand, using the WebBrowser control is a good idea, but why after dropping the first image, the control will not accept another images that means, after dropping the first image, I try to drop another image but it gives nothing.. how can I make it to accept the next image
    Thanks alot for ur help..


  • Umesh_DB2

    I have the same code, I wanna the app to give me the file name.. But after dropping the first image, I can't drop another image in the webBroweser object..
    Here is my code (it's very similar to urs):

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace GetProductID

    {

    public partial class frmGetProductID : Form

    {

    public frmGetProductID()

    {

    InitializeComponent();

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

    {

    string strURL = e.Url.ToString();

    string[] pID = strURL.Split('/');

    pID = pID[pID.Length - 1].Split('[');

    try

    {

    lstProductsIDs.Items.Add(pID[0] + ".JPG");

    }

    catch (FormatException)

    {

    MessageBox.Show("This is not a valid link", "Error", MessageBoxButtons.OK);

    }

    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

    {

    if (!e.Url.ToString().EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))

    e.Cancel = true;

    }

    }

    }



  • Getting Image Source URL