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