Editing How to use Windows clipboard in WPF applications

From Yggenyk
Revision as of 13:21, 25 April 2014 by WikiSysop (talk | contribs)
Jump to navigation Jump to search

WPF application using Clipboard.GetImage

MainWindow.xaml.cs

<source lang="csharp"> using System.IO; using System.Windows; using System.Windows.Controls; using Path = System.IO.Path;

namespace Clipboard_sample {

   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow
   {
       public MainWindow()
       {
           InitializeComponent();
       }
       private void SaveClipboardUsingClipboardGetImage()
       {
           var data = Clipboard.GetDataObject();
           if (data == null)
           {
               MessageBox.Show("Clipboard Empty !!"); 
               return;
           }

           if (data.GetDataPresent(DataFormats.Bitmap))
           {
               const string directory = @".\";
               var name = _textBox.Text;
               name = string.IsNullOrEmpty(name) && name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 ? "clipboard" : _textBox.Text;
               var format = ((ListBoxItem) (_listBox.SelectedValue)).Content.ToString();
               if (File.Exists(Path.Combine(directory, name + "." + format))) 
               { 
                   MessageBox.Show("The target already exists"); 
               }  
               
               var image = Clipboard.GetImage();
               if (image == null)
               {
                   MessageBox.Show("Cannot get image from clipboard");
                   return;
               }
               switch (format) 
               { 
                   case "Jpeg": 
                       {
                           image.SaveAsJpeg(Path.Combine(directory, name + "." + format)); 
                           break; 
                       } 
                   case "Bmp": 
                       {
                           image.SaveAsBmp(Path.Combine(directory, name + "." + format)); 
                           break; 
                       } 
                   case "Gif": 
                       {
                           image.SaveAsGif(Path.Combine(directory, name + "." + format)); 
                           break; 
                       } 
                   case "Png": 
                       {
                           image.SaveAsPng(Path.Combine(directory, name + "." + format)); 
                           break; 
                       } 
                   case "Tiff": 
                       {
                           image.SaveAsTiff(Path.Combine(directory, name + "." + format)); 
                           break; 
                       } 
                   } 
               } 
               else 
               { 
                   MessageBox.Show("No imagine in Clipboard !!"); 
               } 
       }
       private void _button_OnClick(object sender, RoutedEventArgs e)
       {
           SaveClipboardUsingClipboardGetImage();
       }
   }

} </source>

BitmapExtensions.cs

<source lang="csharp"> using System.IO; using System.Windows.Media.Imaging;

namespace Clipboard_sample {

   static class BitmapExtensions
   {
       public static void SaveAsJpeg(this BitmapSource bitmap, string path, int qualityLevel = 90)
       {
           using (var s = File.Create(path))
           {
               var enc = new JpegBitmapEncoder
               {
                   Frames = { BitmapFrame.Create(bitmap) },
                   QualityLevel = qualityLevel,
               };
               enc.Save(s);
           }
       }
       public static void SaveAsBmp(this BitmapSource bitmap, string path)
       {
           using (var s = File.Create(path))
           {
               var enc = new BmpBitmapEncoder
               {
                   Frames = { BitmapFrame.Create(bitmap) },
               };
               enc.Save(s);
           }
       }
       public static void SaveAsPng(this BitmapSource bitmap, string path)
       {
           using (var s = File.Create(path))
           {
               var enc = new PngBitmapEncoder
               {
                   Frames = { BitmapFrame.Create(bitmap) },
               };
               enc.Save(s);
           }
       }
       public static void SaveAsGif(this BitmapSource bitmap, string path)
       {
           using (var s = File.Create(path))
           {
               var enc = new GifBitmapEncoder
               {
                   Frames = { BitmapFrame.Create(bitmap) },
               };
               enc.Save(s);
           }
       }
       public static void SaveAsTiff(this BitmapSource bitmap, string path)
       {
           using (var s = File.Create(path))
           {
               var enc = new TiffBitmapEncoder
               {
                   Frames = { BitmapFrame.Create(bitmap) },
               };
               enc.Save(s);
           }
       }
   }

} </source>

Windows Store App

Clipboard app sample This sample covers the following:

  • How to copy and paste text
    1. How to copy and paste an image
    2. How to copy and paste files
    3. How to get the formats on the Clipboard
    4. How to detect changes to the Clipboard

<google>ENGELSK</google>

id=siteTree