Reader Level:
Universal Windows Platform

Clipboard Operations in Windows Store Apps Using C#

By Gaurav Gupta on Oct 01 2012
In the article we are going to learn about clipboard operations such as copy, paste and cut in Windows Store apps.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 15k
  • 0

Today we will learn Clipboard operations in Windows Store Apps. The Clipboard includes various types of operations such as cut, copy and paste of data within applications.

A Clipboard operation is the another way or method to share content among multiple applications within the same computer without any interruption. We can perform a Clipboard operation among multiple pages within the same application or between different applications on the same computer.

You can use a Clipboard operation supporting various types of data formats in Windows Store apps. In this article I will show you the Bitmaps data format to perform a Clipboard operation like Copy and Paste of an image within the application.

In the following steps I will show you copying the Bitmap image using an item picker to the clipboard and paste it back to the application from the clipboard.

How to perform clipboard operations.

Step 1

Create a Blank application of Windows Store apps using C#/XAML.

Step 2

In this step I create a XAML page; the source of it is:









    <Grid x:Name="LayoutRoot" >


            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="#FFF7C8C8" Offset="1"/>




            <RowDefinition Height="660"/>

            <RowDefinition Height="auto"/>


        <Grid x:Name="Input" Grid.Row="0" HorizontalAlignment="Center" Margin="0,200">


                <RowDefinition Height="500"/>

                <RowDefinition Height="Auto"/>



                <StackPanel Margin="0,10,0,0">

                    <Button x:Name="selectButton" Content="Select Image" Margin="0,0,0,0"  Click="selectButton_Click_1" Width="130"/>

                    <Button x:Name="CopyButton" Content="Copy" Click="CopyButton_Click_1" Margin="0,40,0,0" Width="130"/>

                    <Button x:Name="PasteButton" Content="Paste" Margin="0,50,0,0" Click="PasteButton_Click_1" Width="130"/>                   





        <Grid x:Name="Output" HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Row="1">

            <StackPanel Orientation="Vertical">

                <TextBlock x:Name ="OutputText" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" Text="" FontSize="25"  />

                <Image x:Name="ImageHolder" HorizontalAlignment="Left" Visibility="Collapsed" MaxHeight="300" Margin="0,30,0,0" MaxWidth="400"/>





Step 3

First you need to add the appropriate namespaces in the MainPage.cs file; they are:

using Windows.ApplicationModel.DataTransfer;

using Windows.Storage;

using Windows.Storage.Pickers;

using Windows.Storage.Streams;

Step 4

Then, I create an object of the DataPackage Class for which we added namespaces in the previous step, as in:

  DataPackage dataPackage = new DataPackage();

The preceding code creates an instance of a DataPackage object that is used to associate the data on which the user wants to perform Clipboard operations.

Step 5

Here is the code of copying a file using FileOpenPicker:

private void CopyButton_Click_1(object sender, RoutedEventArgs e)


   if (imageFile != null)



     OutputText.Text = "Image has been copied";




     catch (Exception ex)


     // Copying data to Clipboard can potentially fail - for example, if another application is holding Clipboard open 




    OutputText.Text = "No image was selected.";


In the preceding code we perform a copy operation on the file. We set the Bitmap to the DataPackage object to be copied. Then, set the content of the image in the clipboard using the SetContent method of the Clipboard Class.

Step 6

Here is code of pasting the content from the Clipboard:

private async void PasteButton_Click_1(object sender, RoutedEventArgs e)


   var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();

   if (dataPackageView.Contains(StandardDataFormats.Bitmap))


       IRandomAccessStreamReference imageReceived = null;



          imageReceived = await dataPackageView.GetBitmapAsync();


       catch (Exception ex)



       if (imageReceived != null)


         using (var imageStream = await imageReceived.OpenReadAsync())


           var bitmapImage = new BitmapImage();


           ImageHolder.Source = bitmapImage;

           ImageHolder.Visibility = Visibility.Visible;

           OutputText.Text = "Image is retrieved from the clipboard and pasted successfully.";






     OutputText.Text = "Bitmap format is not available in clipboard";

     ImageHolder.Visibility = Visibility.Collapsed;


In the preceding code first I get the content from the Clipboard that I previously set using the GetContent method. Then, get that image in Stream format and set the source of the Image control.

Step 7

Now, Run the app and select the image you want to copy.


Click on the Copy button to copy the image to the Clipboard.


Then. click on the Paste button to paste the bitmap from the Clipboard to the application.