-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [WASM] Added the ability to load images from bytes
- Loading branch information
Showing
5 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Controls/ImageTests/LoadFromBytes.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
54 changes: 54 additions & 0 deletions
54
src/SamplesApp/UITests.Shared/Windows_UI_Xaml_Controls/ImageTests/LoadFromBytes.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters