Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Clipboard image" #12599

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions samples/ControlCatalog/Pages/ClipboardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<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" />

Expand Down
73 changes: 1 addition & 72 deletions samples/ControlCatalog/Pages/ClipboardPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
Expand Down Expand Up @@ -119,82 +118,12 @@ private async void PasteFilesDataObject(object? sender, RoutedEventArgs args)
}
}

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 = formats != null ? string.Join(Environment.NewLine, formats) : string.Empty;
ClipboardContent.Text = string.Join(Environment.NewLine, formats);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/Windows/Avalonia.Win32/ClipboardFormats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ public ClipboardFormat(string name, ushort format, params ushort[] synthesized)
#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),
};


Expand Down
22 changes: 1 addition & 21 deletions src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,37 +2135,17 @@ public enum ClipboardFormat
/// </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 = 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,
CF_DIB = 3,
/// <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
Expand Down