Browse Source

Merge pull request #12246 from viordash/clipboard-image

Clipboard image
pull/12599/head
Max Katz 3 years ago
committed by GitHub
parent
commit
144e2ce37b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      samples/ControlCatalog/Pages/ClipboardPage.xaml
  2. 73
      samples/ControlCatalog/Pages/ClipboardPage.xaml.cs
  3. 7
      src/Windows/Avalonia.Win32/ClipboardFormats.cs
  4. 22
      src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs

3
samples/ControlCatalog/Pages/ClipboardPage.xaml

@ -12,6 +12,9 @@
<Button Click="CopyFilesDataObject" Content="Copy files to clipboard (data object)" />
<Button Click="PasteFilesDataObject" Content="Paste files from clipboard (data object)" />
<Button Click="CopyBitmapDataObject" Content="Copy image to clipboard (data object)" ToolTip.Tip="CF_BITMAP, CF_DIB, image/bmp, image/png, image/jpeg" />
<Button Click="PasteBitmapDataObject" Content="Paste image from clipboard (data object)" ToolTip.Tip="CF_BITMAP, CF_DIB, image/bmp, image/png, image/jpeg" />
<Button Click="GetFormats" Content="Get clipboard formats" />
<Button Click="Clear" Content="Clear clipboard" />

73
samples/ControlCatalog/Pages/ClipboardPage.xaml.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
@ -118,12 +119,82 @@ namespace ControlCatalog.Pages
}
}
private async void CopyBitmapDataObject(object? sender, RoutedEventArgs args)
{
if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
{
var lines = (ClipboardContent.Text ?? string.Empty)
.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 3)
{
return;
}
var format = lines[0];
var hexLine = string.Join(" ", lines.Skip(2));
var hexBytes = hexLine.Split(new[] { ' ', '-' }, StringSplitOptions.RemoveEmptyEntries);
var bytes = hexBytes.Select(x => Convert.ToByte(x, 16)).ToArray();
var dataObject = new DataObject();
dataObject.Set(format, bytes);
await clipboard.SetDataObjectAsync(dataObject);
}
}
private async void PasteBitmapDataObject(object? sender, RoutedEventArgs args)
{
if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
{
var format = "Bitmap";
var obj = await clipboard.GetDataAsync(format);
var bytes = obj as IEnumerable<byte>;
if (bytes == null)
{
format = "Dib";
obj = await clipboard.GetDataAsync(format);
bytes = obj as IEnumerable<byte>;
}
if (bytes == null)
{
format = "image/bmp";
obj = await clipboard.GetDataAsync(format);
bytes = obj as IEnumerable<byte>;
}
if (bytes == null)
{
format = "image/png";
obj = await clipboard.GetDataAsync(format);
bytes = obj as IEnumerable<byte>;
}
if (bytes == null)
{
format = "image/jpeg";
obj = await clipboard.GetDataAsync(format);
bytes = obj as IEnumerable<byte>;
}
if (bytes != null)
{
var printable = bytes.ToArray();
var sb = new StringBuilder(256 + printable.Length * 3);
sb.AppendLine($"{format}");
sb.AppendLine($"{printable.Length} bytes");
sb.Append(BitConverter.ToString(printable).Replace('-', ' '));
ClipboardContent.TextWrapping = Avalonia.Media.TextWrapping.NoWrap;
ClipboardContent.Text = sb.ToString();
ClipboardContent.TextWrapping = Avalonia.Media.TextWrapping.Wrap;
}
else
{
ClipboardContent.Text = string.Empty;
}
}
}
private async void GetFormats(object sender, RoutedEventArgs args)
{
if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
{
var formats = await clipboard.GetFormatsAsync();
ClipboardContent.Text = string.Join(Environment.NewLine, formats);
ClipboardContent.Text = formats != null ? string.Join(Environment.NewLine, formats) : string.Empty;
}
}

7
src/Windows/Avalonia.Win32/ClipboardFormats.cs

@ -33,6 +33,13 @@ namespace Avalonia.Win32
#pragma warning disable CS0618 // Type or member is obsolete
new ClipboardFormat(DataFormats.FileNames, (ushort)UnmanagedMethods.ClipboardFormat.CF_HDROP),
#pragma warning restore CS0618 // Type or member is obsolete
new ClipboardFormat("Bitmap", (ushort)UnmanagedMethods.ClipboardFormat.CF_BITMAP),
new ClipboardFormat("MetafilePict", (ushort)UnmanagedMethods.ClipboardFormat.CF_METAFILEPICT),
new ClipboardFormat("Dib", (ushort)UnmanagedMethods.ClipboardFormat.CF_DIB, (ushort)UnmanagedMethods.ClipboardFormat.CF_DIBV5),
new ClipboardFormat("EnhancedMetafile", (ushort)UnmanagedMethods.ClipboardFormat.CF_ENHMETAFILE),
new ClipboardFormat("Palette", (ushort)UnmanagedMethods.ClipboardFormat.CF_PALETTE),
new ClipboardFormat("PenData", (ushort)UnmanagedMethods.ClipboardFormat.CF_PENDATA),
};

22
src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs

@ -2135,17 +2135,37 @@ namespace Avalonia.Win32.Interop
/// </summary>
CF_BITMAP = 2,
/// <summary>
/// Handle to a metafile picture format as defined by the METAFILEPICT structure. When passing a CF_METAFILEPICT handle by means of DDE, the application responsible for deleting hMem should also free the metafile referred to by the CF_METAFILEPICT handle.
/// </summary>
CF_METAFILEPICT = 3,
/// <summary>
/// A memory object containing a BITMAPINFO structure followed by the bitmap bits.
/// </summary>
CF_DIB = 3,
CF_DIB = 8,
/// <summary>
/// Handle to a color palette.
/// </summary>
CF_PALETTE = 9,
/// <summary>
/// Data for the pen extensions to the Microsoft Windows for Pen Computing.
/// </summary>
CF_PENDATA = 10,
/// <summary>
/// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data.
/// </summary>
CF_UNICODETEXT = 13,
/// <summary>
/// A handle to an enhanced metafile (HENHMETAFILE).
/// </summary>
CF_ENHMETAFILE = 14,
/// <summary>
/// A handle to type HDROP that identifies a list of files.
/// </summary>
CF_HDROP = 15,
/// <summary>
/// A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits.
/// </summary>
CF_DIBV5 = 17,
}
public struct MSG

Loading…
Cancel
Save