【问题标题】:Drag & Drop from internet browser (winforms)从互联网浏览器拖放(winforms)
【发布时间】:2017-02-24 22:23:31
【问题描述】:

我正在制作的应用程序使用户能够将图片拖到其上,然后将此图像一次复制到多个文件夹中。 当我使用本地文件时,一切都很好,但我不能让它与从互联网浏览器直接拖到表单上的图像一起工作。

当然,图像保存在正确的文件夹中,但名称被更改为带有 .bmp 扩展名的乱码。

例如,当我从浏览器中拖动 100kb 名称为“image.jpg”的图像时,我得到了 3mb 名称为“sInFeER.bmp”的图像。我正在火狐上测试它。我知道当我这样做时我实际上是从浏览器临时文件夹中复制图像并且文件名已经在那里更改了。

在这种情况下如何保持正确的文件名和扩展名? (最好不要将图像转换为巨大的 bmp 文件...)

我用于测试的代码:

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
            }
        }

【问题讨论】:

  • 你拖拽图片的应用已经改变了文件名,所以你的接收winform应用不能再获取原来的文件名了。
  • 是的,看起来是这样...但是当我将相同的图像从浏览器拖到桌面时,文件名和扩展名与原始文件名和扩展名相同(但我猜浏览器正在这里下载)。也许我应该使用 WebClient.DownloadFile 呢?那么现在的问题是如何获取拖拽图片的url地址?
  • 浏览器通常以多种格式提供被拖动的对象。您对 DataFormats.FileDrop 很满意,所以这就是您所得到的。如果您想以自己的图像格式存储图像,则必须寻找另一种格式。查找 DataFormats.Bitmap 和 DataFormats.Dib。后者有点棘手,示例代码is here

标签: c# winforms browser drag-and-drop


【解决方案1】:

感谢所有回答。

我做了一些阅读,并决定从浏览器 (Firefox) 拖动时访问原始图像几乎是不可能的(不使用 Firefox API),所以我只是使用 WebClient.DownloadFile 来下载拖放的图片。

这是我结束的代码:

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;
        }

可能有更好的方法,但这对我有用。似乎不适用于其他浏览器。 (但我只需要它来使用 Firefox,所以我不在乎)

【讨论】:

    【解决方案2】:

    看起来您正在将像素从 Firefox 拖到您的应用程序,而不是 url。我会在 mozilla site 上查看有关如何将 url 拖到您的应用程序的更多信息。他们有很多编程材料和 API 可以与浏览器交互。

    【讨论】:

    • 谢谢,我一定会阅读这篇文章并在未来尝试实现他们的 API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多