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,

Getting Image Source URL
Wee Bubba
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
Have you tried drag-drop of a hyperlink to see that it drops URLs that you can accept
brian_j
Martin2233
Umesh_DB2
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(){
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){
string strURL = e.Url.ToString(); string[] pID = strURL.Split('/');pID = pID[pID.Length - 1].Split(
'['); try{
".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)) true;}
}
}