#701 – Dragging an Image Between WPF Applications
November 29, 2012 1 Comment
You can use drag and drop functionality in WPF to drag an image from one application to another.
In the drag source, you create a DataObject with a bitmap format. (Click here for ImageToBitmap source code).
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Bitmap bitmap = ImageToBitmap(e.Source as System.Windows.Controls.Image); DataObject data = new DataObject(DataFormats.Bitmap, bitmap); DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy); }
In the drop target (the application where you want to drop the image), you just set the Source property of an existing Image control to the data obtained from the GetData method.
<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Drop Target" Height="350" Width="325" AllowDrop="True" Drop="Window_Drop"> <Grid> <Image Name="imgDropHere"/> </Grid> </Window>
private void Window_Drop(object sender, DragEventArgs e) { BitmapSource bmSource = (BitmapSource)e.Data.GetData(DataFormats.Bitmap); imgDropHere.Source = bmSource; }