tags:

views:

55

answers:

1

I can load images in WPF by using the BitmapImage class. My code works perfectly when I drag an image within a WrapPanel but its throwing an exception while I'm going to drop that dragged image on another image within the same WrapPanel.Can anybody suggest me for good code else rectify it plz

My XAML is:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="Window1" Height="446" Width="580" Loaded="Window_Loaded">
    <Grid>
        <WrapPanel Margin="31,24,33,39" Name="wrapPanel1" AllowDrop="True">
        </WrapPanel>
    </Grid>
</Window>

and My C# is:

public partial class Window1 : Window
{
    Image dragSource = null;

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        FillWrapPanel();
    }

    private void FillWrapPanel()
    {
        DirectoryInfo di = new DirectoryInfo("C:\\Image");
        FileInfo[] fi = di.GetFiles();
        int i = 1;
        foreach (FileInfo finfo in fi)
        {
            Image img = new Image();
            img.Name = "Image" + i.ToString();
            i++;
            img.AllowDrop = true;
            img.MouseDown += new MouseButtonEventHandler(img_MouseDown);
            img.DragEnter += new DragEventHandler(img_DragEnter);
            img.Drop += new DragEventHandler(img_Drop);
            img.Height = 150;
            img.Width = 150;
            BitmapImage bimg = new BitmapImage(new Uri(finfo.FullName));
            img.Source = bimg;
            wrapPanel1.Children.Add(img);
        }
    }

    void img_Drop(object sender, DragEventArgs e)
    {
        object data = e.Data.GetData(typeof(Image));
        e.Data.SetData(null);
        Image img = (Image)data;

        if (wrapPanel1.Children.Contains(img))
        {
            int i = wrapPanel1.Children.IndexOf((Image)sender);
            wrapPanel1.Children.Insert(i, img);
        }
    }

    void img_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Image parent = (Image)sender;

        dragSource = parent;

        object data = new DataObject(typeof(Image), parent);

        try
        {
            DragDrop.DoDragDrop(parent, data, DragDropEffects.Copy);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    void img_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effects = DragDropEffects.Copy;
        else
            e.Effects = DragDropEffects.None;
    }

    //gets the object for the element selected (from the point) in the WrapPanel (source)
    private static object GetObjectDataFromPoint(Image source, Point point, out UIElement dataContainer)
    {
        dataContainer = null;
        UIElement element = source.InputHitTest(point) as UIElement;
        if (element != null)
        {
            //get the object from the element
            object data = DependencyProperty.UnsetValue;
            while (data == DependencyProperty.UnsetValue)
            {
                // try to get the object value for the corresponding element

                data = element;

                //get the parent and we will iterate again
                if (data == DependencyProperty.UnsetValue)
                    element = VisualTreeHelper.GetParent(element) as UIElement;

                //if we reach the actual WrapPanel then we must break to avoid an infinite loop
                if (element == source)
                    return null;
            }

            //return the data that we fetched only if it is not Unset value, which would mean that we did not find the data
            if (data != DependencyProperty.UnsetValue)
            {
                dataContainer = element;
                return data;
            }
        }

        return null;
    }
}
A: 

My XAML is:

Title="Window1" Height="446" Width="580" Loaded="Window_Loaded">

and My C# is:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO;

namespace WpfApplication1 { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { Image dragSource = null; int j = 0;

    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        FillWrapPanel();
    }

    private void FillWrapPanel()
    {
        DirectoryInfo di = new DirectoryInfo(@"C:\Documents and Settings\basanta\Desktop\Images");
        FileInfo[] fi = di.GetFiles();
        int i = 0;
        foreach (FileInfo finfo in fi)
        {
           if(finfo.Extension == ".jpg")
           {
            Image img = new Image();
            img.Name = "Image" + i.ToString();
            i++;
            img.AllowDrop = true;
            img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
            img.DragEnter += new DragEventHandler(img_DragEnter);
            img.Drop += new DragEventHandler(img_Drop);
            img.Height = 150;
            img.Width = 150;
            img.Source = new BitmapImage(new Uri(finfo.FullName));
            wrapPanel1.Children.Add(img);
           }
        }
    }

    void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Image parent = (Image)sender;
        j=wrapPanel1.Children.IndexOf((Image)sender);

        dragSource = parent;

        object data = new DataObject(typeof(Image), parent);

        DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
    }

    void img_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effects = DragDropEffects.Move;
        else
            e.Effects = DragDropEffects.None;
    }

    void img_Drop(object sender, DragEventArgs e)
    {
        object data = e.Data.GetData("System.Windows.Controls.Image");
        Image img = (Image)data;

        if (wrapPanel1.Children.Contains(img))
        {
            int i = wrapPanel1.Children.IndexOf((Image)sender);
            wrapPanel1.Children.RemoveAt(j);
            if (j < i)
            {
                i--;
                wrapPanel1.Children.RemoveAt(i);
                wrapPanel1.Children.Insert(i, img);
            }
            else
            {
                wrapPanel1.Children.RemoveAt(i);
                wrapPanel1.Children.Insert(i, img);
            }
        }
    }
}

}

SharpUrBrain
Can Anybody Optimize this ?
SharpUrBrain
Here in this code I wanted to change the mouse effect after dropping 3 images on a particular image as a blocked sign but its not happening here in this code please suggest me with your good reviews
SharpUrBrain
Share your ideas to optimize it and to work it in a best way
SharpUrBrain
I did dragging dropping on a single item but I am not able to do the same on Multiple items. Somebody help me to find out the way to select multiple items for drag and drop
SharpUrBrain