Clipboard Operations in Windows Store Apps Using C#

Posted by Gaurav Gupta Articles | Windows Store Apps October 01, 2012
In the article we are going to learn about clipboard operations such as copy, paste and cut in Windows Store apps.
Reader Level:

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:

<Page

    x:Class="ClipBoard_operation.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:ClipBoard_operation"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot" >

        <Grid.Background>

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

                <GradientStop Color="Black"/>

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

            </LinearGradientBrush>

        </Grid.Background>

        <Grid.RowDefinitions>

            <RowDefinition Height="660"/>

            <RowDefinition Height="auto"/>

        </Grid.RowDefinitions>

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

            <Grid.RowDefinitions>

                <RowDefinition Height="500"/>

                <RowDefinition Height="Auto"/>

            </Grid.RowDefinitions>

            <StackPanel>              

                <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"/>                   

                </StackPanel>

            </StackPanel>

        </Grid>

 

        <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"/>

            </StackPanel>

        </Grid>

    </Grid>

</Page>

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)

   {                   

     dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageFile));

     OutputText.Text = "Image has been copied";

     try

     {

       Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(dataPackage);

     }
     catch (Exception ex)

     {

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

     }

   }
   else

  {

    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;

       try

       {

          imageReceived = await dataPackageView.GetBitmapAsync();

       }

       catch (Exception ex)

       {

       }

       if (imageReceived != null)

       {

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

         {

           var bitmapImage = new BitmapImage();

           bitmapImage.SetSource(imageStream);

           ImageHolder.Source = bitmapImage;

           ImageHolder.Visibility = Visibility.Visible;

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

        }

      }

   }

  else

  {

     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.

File-Open-Picker-In-Windows-Store-apps.jpg

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

Copy-Bitmap-to-Clipboard-In-Windows-Store-Apps.jpg

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

Paste-from-Clipboard-in-Windows-Store-Apps.jpg

COMMENT USING

Trending up