I have a small WPF application which has a window with an Image control. Image control shows an image from File system. I want user to be able to drag the image and drop to its desktop or anywhere to save it. It's working fine.
But I want to show small image thumbnail along with mouse cursor when user drags it. Just like we drag an image from Windows file explorer to some where else. How to achieve it?
Current Behavior of Drag/Drop
Desired Behavior
Here is my XAML Code
<Grid>
<Image x:Name="img" Height="100" Width="100" Margin="100,30,0,0"/>
</Grid>
Here is C# Code
public partial class MainWindow : Window
{
string imgPath;
Point start;
bool dragStart = false;
public MainWindow()
{
InitializeComponent();
imgPath = "C:\\Pictures\\flower.jpg";
ImageSource imageSource = new BitmapImage(new Uri(imgPath));
img.Source = imageSource;
window.PreviewMouseMove += Window_PreviewMouseMove;
window.PreviewMouseUp += Window_PreviewMouseUp;
window.Closing += Window_Closing;
img.PreviewMouseLeftButtonDown += Img_PreviewMouseLeftButtonDown;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
window.PreviewMouseMove -= Window_PreviewMouseMove;
window.PreviewMouseUp -= Window_PreviewMouseUp;
window.Closing -= Window_Closing;
img.PreviewMouseLeftButtonDown -= Img_PreviewMouseLeftButtonDown;
}
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!dragStart) return;
if (e.LeftButton != MouseButtonState.Pressed)
{
dragStart = false; return;
}
Point mpos = e.GetPosition(null);
Vector diff = this.start - mpos;
if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
string[] file = { imgPath };
DataObject d = new DataObject();
d.SetData(DataFormats.Text, file[0]);
d.SetData(DataFormats.FileDrop, file);
DragDrop.DoDragDrop(this, d, DragDropEffects.Copy);
}
}
private void Img_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.start = e.GetPosition(null);
dragStart = true;
}
private void Window_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
dragStart = false;
}
}


DragDrop.GiveFeedback. Check this stackoverflow.com/questions/4878004/… - Rao Hammas