Skip to content

Commit

Permalink
feat(dragdrop): Add support for RandomAccessStream on BitmapSource
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 16, 2020
1 parent 19894da commit 4f16eed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public partial class BitmapSource : global::Windows.UI.Xaml.Media.ImageSource
// Forced skipping of method Windows.UI.Xaml.Media.Imaging.BitmapSource.BitmapSource()
// Forced skipping of method Windows.UI.Xaml.Media.Imaging.BitmapSource.PixelWidth.get
// Forced skipping of method Windows.UI.Xaml.Media.Imaging.BitmapSource.PixelHeight.get
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void SetSource( global::Windows.Storage.Streams.IRandomAccessStream streamSource)
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Media.Imaging.BitmapSource", "void BitmapSource.SetSource(IRandomAccessStream streamSource)");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncAction SetSourceAsync( global::Windows.Storage.Streams.IRandomAccessStream streamSource)
{
Expand Down
28 changes: 25 additions & 3 deletions src/Uno.UI/UI/Xaml/Media/Imaging/BitmapSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.UI.Core;

namespace Windows.UI.Xaml.Media.Imaging
{
Expand Down Expand Up @@ -60,26 +63,45 @@ public void SetSource(Stream streamSource)
PixelWidth = 0;
PixelHeight = 0;

Stream = streamSource;
var copy = new MemoryStream();
streamSource.CopyTo(copy);
copy.Position = 0;
Stream = copy;

#if NETSTANDARD
InvalidateSource();
#endif
}

public async Task SetSourceAsync(Stream streamSource)
{

if (streamSource != null)
{
PixelWidth = 0;
PixelHeight = 0;

MemoryStream copy = new MemoryStream();
var copy = new MemoryStream();
await streamSource.CopyToAsync(copy);
copy.Position = 0;
Stream = copy;

#if NETSTANDARD
InvalidateSource();
#endif
}
else
{
//Same behavior as windows, although the documentation does not mention it!!!
throw new ArgumentException(nameof(streamSource));
}
}

public void SetSource(IRandomAccessStream streamSource)
// We prefer to use the SetSourceAsync here in order to make sure that the stream is copied ASYNChronously,
// which is important since we are using a stream wrapper of and <In|Out|RA>Stream which might freeze the UI thread / throw exception.
=> Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => SetSourceAsync(streamSource.GetInputStreamAt(0).AsStreamForRead()));

public IAsyncAction SetSourceAsync(IRandomAccessStream streamSource)
=> AsyncAction.FromTask(ct => SetSourceAsync(streamSource.GetInputStreamAt(0).AsStreamForRead()));
}
}
4 changes: 3 additions & 1 deletion src/Uno.UWP/UI/Composition/SkiaCompositionSurface.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ internal void LoadFromBytes(byte[] image)
try
{
_image = SKImage.FromEncodedData(stream);
return (true, "Success");
return _image is null
? (false, "Failed to decode image")
: (true, "Success");
}
catch (Exception e)
{
Expand Down

0 comments on commit 4f16eed

Please sign in to comment.