diff --git a/samples/ControlCatalog/Pages/ClipboardPage.xaml b/samples/ControlCatalog/Pages/ClipboardPage.xaml
index 4199a9780f..57b3111ab3 100644
--- a/samples/ControlCatalog/Pages/ClipboardPage.xaml
+++ b/samples/ControlCatalog/Pages/ClipboardPage.xaml
@@ -12,6 +12,9 @@
+
+
+
diff --git a/samples/ControlCatalog/Pages/ClipboardPage.xaml.cs b/samples/ControlCatalog/Pages/ClipboardPage.xaml.cs
index 089d1af197..b4c8544a72 100644
--- a/samples/ControlCatalog/Pages/ClipboardPage.xaml.cs
+++ b/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;
+ if (bytes == null)
+ {
+ format = "Dib";
+ obj = await clipboard.GetDataAsync(format);
+ bytes = obj as IEnumerable;
+ }
+ if (bytes == null)
+ {
+ format = "image/bmp";
+ obj = await clipboard.GetDataAsync(format);
+ bytes = obj as IEnumerable;
+ }
+ if (bytes == null)
+ {
+ format = "image/png";
+ obj = await clipboard.GetDataAsync(format);
+ bytes = obj as IEnumerable;
+ }
+ if (bytes == null)
+ {
+ format = "image/jpeg";
+ obj = await clipboard.GetDataAsync(format);
+ bytes = obj as IEnumerable;
+ }
+
+ 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;
}
}
diff --git a/src/Windows/Avalonia.Win32/ClipboardFormats.cs b/src/Windows/Avalonia.Win32/ClipboardFormats.cs
index 00fdeb2a1d..5d5bad5ffd 100644
--- a/src/Windows/Avalonia.Win32/ClipboardFormats.cs
+++ b/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),
};
diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
index 226ed2a406..f55d7a5f07 100644
--- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
+++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
@@ -2135,17 +2135,37 @@ namespace Avalonia.Win32.Interop
///
CF_BITMAP = 2,
///
+ /// 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.
+ ///
+ CF_METAFILEPICT = 3,
+ ///
/// A memory object containing a BITMAPINFO structure followed by the bitmap bits.
///
- CF_DIB = 3,
+ CF_DIB = 8,
+ ///
+ /// Handle to a color palette.
+ ///
+ CF_PALETTE = 9,
+ ///
+ /// Data for the pen extensions to the Microsoft Windows for Pen Computing.
+ ///
+ CF_PENDATA = 10,
///
/// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data.
///
CF_UNICODETEXT = 13,
///
+ /// A handle to an enhanced metafile (HENHMETAFILE).
+ ///
+ CF_ENHMETAFILE = 14,
+ ///
/// A handle to type HDROP that identifies a list of files.
///
CF_HDROP = 15,
+ ///
+ /// A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits.
+ ///
+ CF_DIBV5 = 17,
}
public struct MSG