0

Application I'm making give user ability to drag picture onto it and then copy this image to several folders at once. Everything works great when I use local file, but I can't make it work with images dragged from internet browser directly on form.

Sure, image is saved in correct folders but name is changed to gibberish with .bmp extension.

So for example when I drag 100kb image with name "image.jpg" from browser I get 3mb image with name "sInFeER.bmp". I'm testing it on Firefox. I know I'm actually copying image from browser temp folder when I do that and file name is already changed there.

How can I keep correct file name and extension in this situation? (and preferably get image NOT converted to huge bmp file...)

Code I'm using for testing:

private void button10_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.All;
            }
        }

private void button10_DragDrop(object sender, DragEventArgs e)
        {
            string[] draggedFiles = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            CatchFile(0, draggedFiles);
        }

private void CatchFile(int id, string[] draggedFiles)
        {
            string directory = @”C:\test\”;

            foreach (string file in draggedFiles)
            {
                Directory.CreateDirectory(directory);
                File.Copy(file, directory + Path.GetFileName(file));

                MessageBox.Show(Path.GetFileName(file)); // test
            }
        }
Legalt
  • 11
  • 3
  • the app from where you are dragging the image has changed the file name, so your receiving winform app cannot get the original file name anymore. – Roland Feb 23 '17 at 13:36
  • Yeah it looks that way... But when I drag same image from browser to desktop, file name and extension is same as original (but I guess browser is doing downloading here). Maybe I should use WebClient.DownloadFile then? So now the question is how can I get url address of dragged image? – Legalt Feb 23 '17 at 14:04
  • A browser generally makes dragged objects available in multiple formats. You were happy with DataFormats.FileDrop so that's what you got. If you want to store the image in your own image format then you'll have to look for another format. Look for DataFormats.Bitmap and DataFormats.Dib. The latter is a bit tricky, sample code [is here](http://stackoverflow.com/a/11274109/17034). – Hans Passant Feb 24 '17 at 10:05

2 Answers2

1

Thanks for all answers.

I've done some reading and decided that accessing original image when dragging from browser (Firefox) is pretty much impossible (without use of Firefox API), so I just used WebClient.DownloadFile to download dropped picture.

Here is code I ended with:

private void button10_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.All;
            }
        }

        private void button10_DragDrop(object sender, DragEventArgs e)
        {
            string draggedFileUrl = (string)e.Data.GetData(DataFormats.Html, false);
            string[] draggedFiles = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            CatchFile(draggedFiles, draggedFileUrl);
        }

        private void CatchFile(string[] draggedFiles, string draggedFileUrl)
        {
            string directory = @"C:\test\";

            foreach (string file in draggedFiles)
            {
                Directory.CreateDirectory(directory);

                if (string.IsNullOrEmpty(draggedFileUrl))
                {
                    if (!File.Exists(directory + Path.GetFileName(file))) File.Copy(file, directory + Path.GetFileName(file));
                    else
                    {
                        MessageBox.Show("File with that name already exists!");
                    }
                }
                else
                {
                    string fileUrl = GetSourceImage(draggedFileUrl);
                    if (!File.Exists(directory + Path.GetFileName(fileUrl)))
                    {
                        using (var client = new WebClient())
                        {
                            client.DownloadFileAsync(new Uri(fileUrl), directory + Path.GetFileName(fileUrl));
                        }
                    }
                    else
                    {
                        MessageBox.Show("File with that name already exists!");
                    }
                }

                // Test check:
                if (string.IsNullOrEmpty(draggedFileUrl)) MessageBox.Show("File dragged from hard drive.\n\nName:\n" + Path.GetFileName(file));
                else MessageBox.Show("File dragged frow browser.\n\nName:\n" + Path.GetFileName(GetSourceImage(draggedFileUrl)));
            }
        }

        private string GetSourceImage(string str)
        {
            string finalString = string.Empty;
            string firstString = "src=\"";
            string lastString = "\"";

            int startPos = str.IndexOf(firstString) + firstString.Length;
            string modifiedString = str.Substring(startPos, str.Length - startPos);
            int endPos = modifiedString.IndexOf(lastString);
            finalString = modifiedString.Substring(0, endPos);

            return finalString;
        }

There is probably better way of doing that but this works for me. Doesn't seem to work with other browsers. (but I needed it only to work with Firefox so I don't care)

Legalt
  • 11
  • 3
0

Looks like you were dragging pixels from firefox to your app, not the url. I would look on the mozilla site for more info on how to drag the url to your app. They have lots of programming stuff and APIs to interact with the browser.

Roland
  • 4,619
  • 7
  • 49
  • 81