Skip to content
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
1 change: 1 addition & 0 deletions Files/Files.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<Compile Include="Extensions\EnumExtensions.cs" />
<Compile Include="Extensions\ImageSourceExtensions.cs" />
<Compile Include="Extensions\TaskExtensions.cs" />
<Compile Include="Helpers\SaveImageToFile.cs" />
<Compile Include="Filesystem\FolderHelpers.cs" />
<Compile Include="Filesystem\LibraryLocationItem.cs" />
<Compile Include="Filesystem\LibraryManager.cs" />
Expand Down
13 changes: 10 additions & 3 deletions Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Files.Filesystem.FilesystemHistory;
using Files.Helpers;
using Files.Interacts;
using Files.UserControls;
using Files.ViewModels;
using Files.ViewModels.Dialogs;
using Microsoft.Toolkit.Uwp;
Expand All @@ -19,6 +18,7 @@
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation.Collections;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using static Files.Helpers.NativeFindStorageItemHelper;
Expand Down Expand Up @@ -625,8 +625,15 @@ public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageVi
// Set the name of the file to be the current time and date
var file = await folder.CreateFileAsync($"{DateTime.Now:mm-dd-yy-HHmmss}.png", CreationCollisionOption.GenerateUniqueName);

using var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
await imageStream.AsStreamForRead().CopyToAsync(stream.AsStreamForWrite());
SoftwareBitmap softwareBitmap;

// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);

// Get the SoftwareBitmap representation of the file
softwareBitmap = await decoder.GetSoftwareBitmapAsync();

await Helpers.SaveImageToFile.SaveSoftwareBitmapToFile(softwareBitmap, file, BitmapEncoder.PngEncoderId);
return ReturnResult.Success;
}
catch (Exception)
Expand Down
55 changes: 55 additions & 0 deletions Files/Helpers/SaveImageToFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;

namespace Files.Helpers
{
static class SaveImageToFile
{
/// <summary>
/// This function encodes a software bitmap with the specified encoder and saves it to a file
/// </summary>
/// <param name="softwareBitmap"></param>
/// <param name="outputFile"></param>
/// <param name="encoderId">The guid of the image encoder type</param>
/// <returns></returns>
public static async Task SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile, Guid encoderId)
{
using IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);
// Create an encoder with the desired format
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, stream);

// Set the software bitmap
encoder.SetSoftwareBitmap(softwareBitmap);

try
{
await encoder.FlushAsync();
}
catch (Exception err)
{
const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked((int)0x88982F81);
switch (err.HResult)
{
case WINCODEC_ERR_UNSUPPORTEDOPERATION:
// If the encoder does not support writing a thumbnail, then try again
// but disable thumbnail generation.
encoder.IsThumbnailGenerated = false;
break;
default:
throw;
}
}

if (encoder.IsThumbnailGenerated == false)
{
await encoder.FlushAsync();
}
}
}
}