I'm trying to use a Blazor input file, and also the Imagesharp library, to turn an IBrowserFile into an image.
My method looks like this
public async Task<byte[]> ConvertFileToByteArrayAsync(IBrowserFile file)
{
using var image = Image.Load(file.OpenReadStream());
image.Mutate(x => x.Resize(new ResizeOptions
{
Mode = ResizeMode.Min,
Size = new Size(128)
}));
MemoryStream memoryStream = new MemoryStream();
if (file.ContentType == "image/png")
{
await image.SaveAsPngAsync(memoryStream);
}
else
{
await image.SaveAsJpegAsync(memoryStream);
}
var byteFile = memoryStream.ToArray();
memoryStream.Close();
memoryStream.Dispose();
return byteFile;
}
But I'm getting the following error :
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Synchronous reads are not supported.
System.NotSupportedException: Synchronous reads are not supported.
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
at SixLabors.ImageSharp.Image.WithSeekableStream[ValueTuple`2](Configuration configuration, Stream stream, Func`2 action)
at SixLabors.ImageSharp.Image.Load(Configuration configuration, Stream stream, IImageFormat& format)
at SixLabors.ImageSharp.Image.Load(Configuration configuration, Stream stream)
at SixLabors.ImageSharp.Image.Load(Stream stream)
at MasterMealWA.Client.Services.FileService.ConvertFileToByteArrayAsync(IBrowserFile file) in F:\CoderFoundry\Code\MasterMealWA\MasterMealWA\Client\Services\FileService.cs:line 37
at MasterMealWA.Client.Pages.RecipePages.RecipeCreate.CreateRecipeAsync() in F:\CoderFoundry\Code\MasterMealWA\MasterMealWA\Client\Pages\RecipePages\RecipeCreate.razor:line 128
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
for the record, line 37 is "using var image......", I don't quite see where I'm using multiple streams, unless its the read stream and memory stream. however, I also don't see how to close the stream I open with file.OpenReadStream.