Skip to content

Commit 707f530

Browse files
authored
Added ability to paste images from clipboard directly into folder (#3235)
1 parent aeb7e1e commit 707f530

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

Files/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void Clipboard_ContentChanged(object sender, object e)
247247
// Clipboard.GetContent() will throw UnauthorizedAccessException
248248
// if the app window is not in the foreground and active
249249
DataPackageView packageView = Clipboard.GetContent();
250-
if (packageView.Contains(StandardDataFormats.StorageItems))
250+
if (packageView.Contains(StandardDataFormats.StorageItems) || packageView.Contains(StandardDataFormats.Bitmap))
251251
{
252252
App.InteractionViewModel.IsPasteEnabled = true;
253253
}

Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
using System.Threading;
1414
using System.Threading.Tasks;
1515
using Windows.ApplicationModel.DataTransfer;
16+
using Windows.Graphics.Imaging;
1617
using Windows.Storage;
18+
using Windows.Storage.Streams;
1719
using static Files.Helpers.NativeFindStorageItemHelper;
1820
using FileAttributes = System.IO.FileAttributes;
1921

@@ -505,33 +507,53 @@ public async Task<ReturnResult> CopyItemAsync(IStorageItemWithPath source, strin
505507

506508
public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageView, string destination, bool registerHistory)
507509
{
508-
if (!packageView.Contains(StandardDataFormats.StorageItems))
510+
if (packageView.Contains(StandardDataFormats.StorageItems))
509511
{
510-
// Happens if you copy some text and then you Ctrl+V in Files
511-
// Should this be done in ModernShellPage?
512-
return ReturnResult.BadArgumentException;
513-
}
512+
IReadOnlyList<IStorageItem> source;
513+
try
514+
{
515+
source = await packageView.GetStorageItemsAsync();
516+
}
517+
catch (Exception ex) when ((uint)ex.HResult == 0x80040064 || (uint)ex.HResult == 0x8004006A)
518+
{
519+
return ReturnResult.UnknownException;
520+
}
521+
ReturnResult returnStatus = ReturnResult.InProgress;
514522

515-
IReadOnlyList<IStorageItem> source;
516-
try
517-
{
518-
source = await packageView.GetStorageItemsAsync();
519-
}
520-
catch (Exception ex) when ((uint)ex.HResult == 0x80040064 || (uint)ex.HResult == 0x8004006A)
521-
{
522-
return ReturnResult.UnknownException;
523+
List<string> destinations = new List<string>();
524+
foreach (IStorageItem item in source)
525+
{
526+
destinations.Add(Path.Combine(destination, item.Name));
527+
}
528+
529+
returnStatus = await CopyItemsAsync(source, destinations, registerHistory);
530+
531+
return returnStatus;
523532
}
524-
ReturnResult returnStatus = ReturnResult.InProgress;
525533

526-
List<string> destinations = new List<string>();
527-
foreach (IStorageItem item in source)
534+
if (packageView.Contains(StandardDataFormats.Bitmap))
528535
{
529-
destinations.Add(Path.Combine(destination, item.Name));
536+
try
537+
{
538+
var imgSource = await packageView.GetBitmapAsync();
539+
using var imageStream = await imgSource.OpenReadAsync();
540+
var folder = await StorageFolder.GetFolderFromPathAsync(destination);
541+
// Set the name of the file to be the current time and date
542+
var file = await folder.CreateFileAsync($"{DateTime.Now:mm-dd-yy-HHmmss}.png", CreationCollisionOption.GenerateUniqueName);
543+
544+
using var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
545+
await imageStream.AsStreamForRead().CopyToAsync(stream.AsStreamForWrite());
546+
return ReturnResult.Success;
547+
}
548+
catch (Exception)
549+
{
550+
return ReturnResult.UnknownException;
551+
}
530552
}
531553

532-
returnStatus = await CopyItemsAsync(source, destinations, registerHistory);
533-
534-
return returnStatus;
554+
// Happens if you copy some text and then you Ctrl+V in Files
555+
// Should this be done in ModernShellPage?
556+
return ReturnResult.BadArgumentException;
535557
}
536558

537559
#endregion Copy

0 commit comments

Comments
 (0)