Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop file transfer in WPF. How to get file name of the dropped content in a window

I'm creating a local file transfer app. I would like the user to drag-drop an item into the file transfer application to initiate the file transfer just like skype or other messengers.

While dropping an item. The drop event was triggered. But, I don't know where to get the details of the item such as Location, Size etc., eg., If I drop an Image. I want to read the details mentioned above.

Note:I have enabled the AllowDrop & Subsribed to Drop event.[If that helps]

like image 456
Prince Ashitaka Avatar asked Oct 28 '10 06:10

Prince Ashitaka


1 Answers

Do you mean Size of file or Size of image in pixel? Anyway, use this code:

private void Window_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop, true))
        {
            string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (var path in droppedFilePaths)
            {          
                string location = null;
                int pxWidth = 0, pxHeight = 0;

                FileInfo fi = new FileInfo(path);
                //fi.Length  //File size
                //fi.DirectoryName //Directory
                using (var fs = fi.OpenRead())
                {
                    try
                    {
                        var bmpFrame = BitmapFrame.Create(fs);
                        var m = bmpFrame.Metadata as BitmapMetadata;
                        if (m != null)
                            location = m.Location;
                        pxWidth = bmpFrame.PixelWidth;
                        pxHeight = bmpFrame.PixelHeight;
                    }
                    catch
                    {
                        //File isn't image
                    }
                }

                this.fileList.Items.Add(string.Format("({0}x{1}), location: {2}", pxWidth, pxHeight, location));
            }
        }
    }
like image 167
vortexwolf Avatar answered Sep 29 '22 10:09

vortexwolf