Browse Source

implement CopyBitmapDataObject

pull/12246/head
Vitaliy Orazov 3 years ago
parent
commit
0ec1c44d07
  1. 5
      samples/ControlCatalog/Pages/ClipboardPage.xaml
  2. 34
      samples/ControlCatalog/Pages/ClipboardPage.xaml.cs

5
samples/ControlCatalog/Pages/ClipboardPage.xaml

@ -12,7 +12,7 @@
<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 bitmap (CF_DIB, image/bmp) to clipboard (data object)" IsEnabled="False" />
<Button Click="CopyBitmapDataObject" Content="Copy bitmap (CF_DIB, image/bmp) to clipboard (data object)" />
<Button Click="PasteBitmapDataObject" Content="Paste bitmap (CF_DIB, image/bmp) from clipboard (data object)" />
<Button Click="GetFormats" Content="Get clipboard formats" />
@ -21,7 +21,6 @@
<TextBox x:Name="ClipboardContent"
MinHeight="400"
AcceptsReturn="True"
Watermark="Text to copy of file names per line"
TextWrapping="Wrap"/>
Watermark="Text to copy of file names per line" />
</StackPanel>
</UserControl>

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

@ -119,9 +119,24 @@ namespace ControlCatalog.Pages
}
}
private void CopyBitmapDataObject(object? sender, RoutedEventArgs args)
private async void CopyBitmapDataObject(object? sender, RoutedEventArgs args)
{
throw new NotSupportedException();
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));
var dataObject = new DataObject();
dataObject.Set(format, bytes);
await clipboard.SetDataObjectAsync(dataObject);
}
}
private async void PasteBitmapDataObject(object? sender, RoutedEventArgs args)
@ -133,6 +148,12 @@ namespace ControlCatalog.Pages
var obj = await clipboard.GetDataAsync(format);
var bytes = obj as IEnumerable<byte>;
if (bytes == null)
{
format = "Unknown_Format_8";
obj = await clipboard.GetDataAsync(format);
bytes = obj as IEnumerable<byte>;
}
if (bytes == null)
{
format = "Dib";
obj = await clipboard.GetDataAsync(format);
@ -159,11 +180,14 @@ namespace ControlCatalog.Pages
if (bytes != null)
{
var printable = bytes.Take(1000);
var printable = bytes.ToArray();
var sb = new StringBuilder(256 + printable.Count() * 3);
sb.AppendLine($"format: {format}, {bytes.Count()} bytes, list of first {printable.Count()}");
sb.AppendJoin(" ", printable.Select(x => x.ToString("X2")));
sb.AppendLine($"{format}");
sb.AppendLine($"{bytes.Count()} bytes (list of first {printable.Count()} bytes)");
sb.Append(BitConverter.ToString(printable).Replace('-', ' '));
ClipboardContent.TextWrapping = Avalonia.Media.TextWrapping.NoWrap;
ClipboardContent.Text = sb.ToString();
ClipboardContent.TextWrapping = Avalonia.Media.TextWrapping.Wrap;
}
else
{

Loading…
Cancel
Save