#702 – Dragging an Image within a WPF Application

You can drag the contents of one Image control onto another Image control fairly easily.  When you drag an image to another application, you have to convert the image to a bitmap.  But within the same application, you can just use the value of the image’s Source property as the data being dragged.

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Image Grid.Column="0" Source="Life01.jpg" Stretch="None" Margin="10"
                MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
        <Image Grid.Column="1" Margin="10" AllowDrop="True" Drop="Image_Drop" Source="DropHere.png" />
    </Grid>

 

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = e.Source as Image;
            DataObject data = new DataObject(DataFormats.Text, img.Source);

            DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy);
        }

        private void Image_Drop(object sender, DragEventArgs e)
        {
            Image img = e.Source as Image;
            img.Source = (BitmapSource) e.Data.GetData(DataFormats.Text);
        }


Advertisement

#701 – Dragging an Image Between WPF Applications

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

#700 – Dragging an Image to Microsoft Word

You can use the drag and drop functionality in WPF to drag an Image control out of the application.  You can drop this image onto a Word document if you convert the image to an enhanced metafile format.

Here’s the code that initiates the drag operation.

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Bitmap bitmap = ImageToBitmap(e.Source as System.Windows.Controls.Image);

            DataObject data = new DataObject(DataFormats.EnhancedMetafile, MakeMetafileStream(bitmap));

            DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy);
        }

This makes use of a utility function to convert the Image to a Bitmap:

        private Bitmap ImageToBitmap(System.Windows.Controls.Image image)
        {
            RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)image.ActualWidth, (int)image.ActualHeight,
                96.0, 96.0, PixelFormats.Pbgra32);

            image.Measure(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight));
            image.Arrange(new Rect(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight)));

            rtBmp.Render(image);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream stream = new MemoryStream();
            encoder.Frames.Add(BitmapFrame.Create(rtBmp));

            // Save to memory stream and create Bitamp from stream
            encoder.Save(stream);

            return new System.Drawing.Bitmap(stream);
        }

This also requires a utility function that converts a Bitmap to a stream containing a Metafile, taken from Stack Overflow.

        // From http://stackoverflow.com/questions/5270763/convert-an-image-into-wmf-with-net
        private MemoryStream MakeMetafileStream(Bitmap image)
        {
            Graphics graphics = null;
            Metafile metafile = null;
            var stream = new MemoryStream();
            try
            {
                using (graphics = Graphics.FromImage(image))
                {
                    var hdc = graphics.GetHdc();
                    metafile = new Metafile(stream, hdc);
                    graphics.ReleaseHdc(hdc);
                }
                using (graphics = Graphics.FromImage(metafile))
                { graphics.DrawImage(image, 0, 0); }
            }
            finally
            {
                if (graphics != null)
                { graphics.Dispose(); }
                if (metafile != null)
                { metafile.Dispose(); }
            }
            return stream;
        }

Here’s the code in action:

#699 – Converting an Image Control to a Bitmap

You may want to work with the image from an Image control as a standard Windows bitmap.  You can convert the System.Windows.Controls.Image to a System.Drawing.Bitmap as follows (imgLife is an Image control):

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)imgLife.ActualWidth, (int)imgLife.ActualHeight,
                96.0, 96.0, PixelFormats.Pbgra32);

            imgLife.Measure(new System.Windows.Size((int)imgLife.ActualWidth, (int)imgLife.ActualHeight));
            imgLife.Arrange(new Rect(new System.Windows.Size((int)imgLife.ActualWidth, (int)imgLife.ActualHeight)));

            rtBmp.Render(imgLife);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream stream = new MemoryStream();
            encoder.Frames.Add(BitmapFrame.Create(rtBmp));

            // Save to memory stream and create Bitamp from stream
            encoder.Save(stream);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream);

            // Demonstrate that we can do something with the Bitmap
            bitmap.Save(@"D:\Temp\Life.png", ImageFormat.Png);

            // Optionally, if we didn't need Bitmap object, but
            // just wanted to render to file, we could:
            //encoder.Save(new FileStream(@"D:\Temp\Life-Other.png", FileMode.Create));
        }

#698 – Playing an MP3 File Using a MediaPlayer Object

You can use the MediaPlayer class in System.Windows.Media to load and play an MP3 file at runtime.  Here’s an example, where we load and play an MP3 file when the user clicks a button.

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private MediaPlayer mplayer = new MediaPlayer();

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            mplayer.Open(new Uri(@"D:\Temp\Respect.mp3"));
            mplayer.Play();
        }
    }

Notice that we declare the MediaPlayer object within the class, rather than within the Click event handler.  If we declared the object within the handler, the variable would go out of scope when the handler exited and the MediaPlayer object would then eventually get garbage collected–which would stop playback.  So we need to declare it within the class so that the object lives even after the event handler exits.

#697 – Dragging Data Out of Your Application

When you use a user interface element as a drag-and-drop source, you initiate the drag-and-drop operation by calling the DragDrop.DoDragDrop method.  The data that you specify when calling this method can then be dragged onto other elements in your application that act as drop targets.   You can also drag data out to another application that knows how to act as a drag-and-drop target.

In the example below, we left-click and drag on the Image, then dropping data into Microsoft Word.  The actual data dropped is textual.

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string textToDrag = "On November 23, 1936, the first issue of the pictorial magazine Life is published, featuring a cover photo of the Fort Peck Dam by Margaret Bourke-White.";

            DragDrop.DoDragDrop((DependencyObject)e.Source, textToDrag, DragDropEffects.Copy);
        }


#696 – A Drop Target Can Receive Data from Other Applications

When you designate an element in your user interface as a drop target for a drag-and-drop operation, it can receive data that is dragged from other elements in your application, provided that those elements initiate the drag-and-drop operation.  But a drop target element in your application can also serve as a target for a drag source in another application.

You can set the AllowDrop property to true for an element and then add code for the element’s Drop event, where you call IDataObject.GetData to get the dragged data.

In the example below, we’ve made a ListBox the drop target.  We can then drag some text onto it from Microsoft Word.

        private void lbDrop(object sender, DragEventArgs e)
        {
            string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);

            ListBox lbox = e.Source as ListBox;
            lbox.Items.Add(draggedText);
        }

#695 – Implementing Drag-and-Drop Behavior

Below is code that implements some simple drag-and-drop behavior, dragging text from a TextBox to a ListBox.

In the XAML, we specify handlers for the MouseDown event on the TextBox and the Drop event on the ListBox.  We also set the AllowDrop property on the ListBox to true.

        <StackPanel Orientation="Vertical" Grid.Column="0" Margin="10">
            <Label Content="Enter some text and then drag to list"/>
            <TextBox Text="" MouseDown="txtMouseDown"/>
        </StackPanel>
        <ListBox Grid.Column="1" Margin="10" AllowDrop="True" Drop="lbDrop"/>

In the MouseDown event handler for the TextBox, we initiate drag-and-drop using the DragDrop.DoDragDrop method.

        private void txtMouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBox txtElement = e.Source as TextBox;

            DragDrop.DoDragDrop(txtElement, txtElement.SelectedText, DragDropEffects.Copy);
        }

In the Drop event handler for the ListBox, we get the data being dragged and add an item to the ListBox.

        private void lbDrop(object sender, DragEventArgs e)
        {
            string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);

            ListBox lbox = e.Source as ListBox;
            lbox.Items.Add(draggedText);
        }

#694 – An Example of Custom Drag-and-Drop Behavior

You can define your own drag-and-drop behavior in an application, defining the content to be dragged and the controls that the user drags the content from and to.  The basic process for drag-and-drop is:

  • User presses and holds a mouse button (typically left mouse button) on a control that contains the data to be dragged (the source element)
  • User moves mouse pointer over another control, where the data is to be dropped (the target element)
  • User releases mouse button
  • Data is “dropped” onto target control (control is typically updated to reflect new content)

In the example below, the user can select some text in the TextBox and drag it to the ListBox, adding a new item to the list for each chunk of text dragged.

#693 – TextBox Control Supports Drag-and-Drop

The TextBox control in WPF automatically supports drag-and-drop, in several different scenarios.

You can select text in one TextBox and drag it into another.  If you just click the left mouse button on some selected text and drag, the text will be cut from the first TextBox and pasted into the second.

 

If you hold the Ctrl key down while clicking and dragging, you can do a copy operation.

 

You can also drag text from another application (e.g. a web browser) into a TextBox in your application.

 

And you can drag text from a TextBox out of your application, into an application that accepts text as the result of a drag operation.