开发者

How to drag and drop image file in C# WPF application

Hi I'm a beginner in WPF C# and i developed a simple C# WPF application which is a simple window containing an image control and i want to drag any image file from my computer and drop it on the image control for displaying it.And i wrote the code below for dong this

in the XML File

  <Image Height="150" Name="image1" Stretch="Fill" Width="200" AllowDrop="True" Drop="image1_Drop" DragEnter="image1_DragEnter"/> 

and inside the code file (*.cs)

private void image1_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effects=DragDropEffects.Copy;
            else
                e.Effects=DragDropEffects.None;
        } 
    private void image1_Drop(object sender, DragEventArgs e)
        {

        image1.Source = (BitmapImage)e.Data.GetData(DataFormats.Bitmap);
        }
开发者_运维百科

but it doesn't work (when dragging over the image control the cursor become a circle containing back slash) and i don't why could any one hlp thanks in advance


Updated Answer

I have changed the code to the following one but with same problem

private void image1_DragEnter(object sender, DragEventArgs e)
        {

         if(e.Data.GetDataPresent(DataFormats.Text))
                e.Effects=DragDropEffects.Copy;
            else
                e.Effects=DragDropEffects.None;
        }

 private void image1_Drop(object sender, DragEventArgs e)
        {

             string fpath = (string)e.Data.GetData(DataFormats.Text);
             BitmapImage tmpImage=new BitmapImage((new Uri(fpath)));
             image1.Source = tmpImage;    

        }


When dragging a file from the filesystem to a program you do not get Bitmap data in the clipboard. You get the full path to the file, as a string. You need to open the file, read the data and then set the image source.


Are you running your application or Visual Studio that hosts the app under administrative privilege?

If that's the case, the Windows prevents the drag drop operation from happening. In Windows 7 or Vista, an application with a lower security privilege (your Windows Explorer with normal user privilege) cannot send data to another one (your app or Visual Studio with administrative privilege) with a higher security privilege.

Try to do one of the followings:

Run your Visual Studio without administrative privilege. Run your application outside Visual Studio without administrative privilege. Enable and long on to the Administrator account in the OS. In this case, both your Windows Explorer and your Visual Studio will have administrative privileges.

Cheers,

Jacob


I was having the same problem. Ends up VS has this wierd glitch where it doesn't show you the option to set the AllowDrop setting to true so you have to manually code it in. It will not show up in Intellitype but it will make it work!

So pretty much just stick PictureBox.AllowDrop = true; into your code and it will work :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜