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 common.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<!-- Ref: https://learn.microsoft.com/en-us/dotnet/maui/deployment/trimming?view=net-maui-10.0#suppress-analysis-warnings -->
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
29 changes: 28 additions & 1 deletion src/SharedMauiCoreLibrary/Utilities/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,37 @@ public partial class ImageHelper
public static byte[] ToArray(Microsoft.Maui.Graphics.IImage image)
{
using MemoryStream memory = new();

image.Save(memory);
return memory.ToArray();
}
public static byte[] ToArray(FileStream imageStream)
{
if (imageStream is null)
return [];
using MemoryStream memory = new();
imageStream.CopyTo(memory);
return memory.ToArray();
}
public static async Task<byte[]> ToArrayAsync(FileStream imageStream)
{
if (imageStream is null)
return [];
using MemoryStream memory = new();
await imageStream.CopyToAsync(memory);
return memory.ToArray();
}
public static Task<byte[]> ToArrayAsync(string imagePath) => ToArrayAsync(GetImageStream(imagePath));
public static FileStream GetImageStream(string filePath) => File.OpenRead(filePath);
public static Task<FileStream> GetImageStreamAsync(string filePath) => Task.Run(() => File.OpenRead(filePath));
public static async Task<ImageSource?> LoadImageFromStream(string filePath)
{
if (File.Exists(filePath))
{
using FileStream imageStream = await GetImageStreamAsync(filePath).ConfigureAwait(false);
return ImageSource.FromStream(() => imageStream);
}
else return null;
}
#endregion
}
}
Loading