Skip to content

Commit

Permalink
feat: [WASM] Added the ability to load images from bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperJMN committed Mar 26, 2020
1 parent db2ec39 commit 9cfb29f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
Binary file added src/SamplesApp/UITests.Shared/Assets/mslug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ImageTests\LoadFromBytes.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_Margin_On_Container.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3487,6 +3491,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ImageTests\Image_UseTargetSizeLate.xaml.cs">
<DependentUpon>Image_UseTargetSizeLate.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ImageTests\LoadFromBytes.xaml.cs">
<DependentUpon>LoadFromBytes.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ListView\ListView_Margin_On_Container.xaml.cs">
<DependentUpon>ListView_Margin_On_Container.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -5470,6 +5477,7 @@
<Content Include="$(MSBuildThisFileDirectory)Assets\menu.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\MenuBar.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\MenuFlyout.png" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Assets\mslug.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\NavigationView.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\ParallaxView.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\PersonPicture.png" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="Uno.UI.Samples.UITests.ImageTestsControl.LoadFromBytes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="2000"
d:DesignWidth="400">

<StackPanel>
<TextBlock Text="Image loaded from bytes"/>
<Image x:Name="MyImage" Width="400" />
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Uno.UI.Samples.Controls;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Uno.Extensions;

namespace Uno.UI.Samples.UITests.ImageTestsControl
{
[SampleControlInfo("Image", "LoadFromBytes")]
public sealed partial class LoadFromBytes
{
public LoadFromBytes()
{
InitializeComponent();
Loaded += OnLoaded;
}

private async void OnLoaded(object sender, RoutedEventArgs e)
{
var assembly = typeof(LoadFromBytes).Assembly;

var query = from name in assembly.GetManifestResourceNames()
where name.EndsWith("mslug.png")
select assembly.GetManifestResourceStream(name);

using (var stream = query.First())
{
MyImage.Source = await CreateImageSource(stream);
}
}

private async Task<ImageSource> CreateImageSource(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

var image = new BitmapImage();

#if WINDOWS_UWP
await image.SetSourceAsync(stream.AsRandomAccessStream()).AsTask();
#else
var copy = stream.ToMemoryStream();
await image.SetSourceAsync(copy);
#endif
return image;
}
}
}
13 changes: 10 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/Image/Image.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using Windows.Foundation;
using Windows.UI.Xaml.Media;
using Uno.Diagnostics.Eventing;
using Uno.Extensions;
using Uno.Foundation;
using Uno.Logging;
Expand Down Expand Up @@ -102,7 +101,15 @@ private void OnSourceChanged(DependencyPropertyChangedEventArgs e)

_lastMeasuredSize = _zeroSize;

if (source is WriteableBitmap wb)
var stream = source?.Stream;
if (stream != null)
{
stream.Position = 0;
var encodedBytes = Convert.ToBase64String(stream.ReadBytes());
var url = "data:application/octet-stream;base64," + encodedBytes;
SetImageUrl(url);
}
else if (source is WriteableBitmap wb)
{
void setImageContent()
{
Expand Down

0 comments on commit 9cfb29f

Please sign in to comment.