Drag/Drop an Image from a Web Browset

J

JezB

I've implemented a drag & drop operation which allows an image (or a file
containing an image) to be dropped onto a custom control. This works fine on
dragging an image file already on my hard drive, but on dragging an Image
from any web page in a web browser, I'm getting an "Out of memory" exception
thrown, but there appears to be plenty of memory left. Why could this be
happening ?

NB. On dragging a web image, files[0] contains a reference to the image in
my web cache (in short directory format), for example:
"C:\\DOCUME~1\\JEREMYB\\LOCALS~1\\TEMP\\DisplayBannerImage.gif"
Could this be why ? If so how CAN I implement dragging an image from a web
browser ?

Here's the relevant sections of code:

public Form1()
{
InitializeComponent();
AlbumControl ac = new AlbumControl(p, atype, AutoLookup, cwidth /*,
false*/);
ac.AllowDrop = true;
ac.DragDrop += new DragEventHandler(ac_DragDrop);
ac.DragEnter += new DragEventHandler(ac_DragEnter);
}

private void ac_DragDrop(object sender, DragEventArgs e)
{
Image droppedImage = null;
// Handle FileDrop data.
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// Assign the file names to a string array, in
// case the user has selected multiple files.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
try
{
// Assign the first image to the picture variable.
droppedImage = Image.FromFile(files[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
AlbumControl ac = (AlbumControl)sender;
if (droppedImage != null)
{
// DO SOMETHING WITH IMAGE !!
ac.MainImage = droppedImage;
}
// Force the control to be redrawn with the image.
ac.Invalidate();
}

private void ac_DragEnter(object sender, DragEventArgs e)
{
// If the data is a file or a bitmap, display the copy cursor.
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
 
J

JezB

On further investigation, it seems that as the code is fired, the picture
does exist in the web cache but is 0 bytes! It's only a valid picture file
AFTER the code fails and the drag/drop operation is completed. Why ? And
what can be done ?

Should I be trying to load the image from the remote web location rather
than the cache ? If so how can I get at the web address of the picture from
e.Data ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top