[dismiss]
På yggenyk.dk bruger vi cookies til at give dig en god oplevelse og til at indsamle statistik, der kan være med til at forbedre brugeroplevelsen. Hvis du klikker på et link på yggenyk.dk, accepterer du samtidig vores cookiepolitik.
Editing How to use Windows clipboard in WPF applications
Jump to navigation
Jump to search
WPF application using Clipboard.GetImage
MainWindow.xaml
<source lang=XML> <Window x:Class="Clipboard_sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight" Title="MainWindow"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="200"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text =" File name:"/> <TextBox Grid.Column="1" x:Name="_textBox" Text="" /> <Button Grid.Column="2" Content="Save" x:Name="_button" Click="_button_OnClick"/> </Grid> <ListBox x:Name="_listBox" Grid.Row="1" SelectedIndex="0"> <ListBoxItem>Jpeg</ListBoxItem> <ListBoxItem>Bmp</ListBoxItem> <ListBoxItem>Gif</ListBoxItem> <ListBoxItem>Png</ListBoxItem> <ListBoxItem>Tiff</ListBoxItem> <ListBoxItem>Exif</ListBoxItem> <ListBoxItem>Wmf</ListBoxItem> </ListBox> </Grid>
</Window> </source>
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
- How to copy and paste an image
- How to copy and paste files
- How to get the formats on the Clipboard
- How to detect changes to the Clipboard
- How to copy and paste text
<google>ENGELSK</google>