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


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