gpt4 book ai didi

c# - 为什么我得到 ImageSharp 不支持同步读取?

转载 作者:行者123 更新时间:2023-12-05 09:04:04 28 4
gpt4 key购买 nike

我正在尝试使用 Blazor 输入文件以及 Imagesharp 库将 IBrowserFile 转换为图像。
我的方法是这样的

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;

}

但我收到以下错误:

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)

记录一下,第 37 行是“使用 var 图像……”,我不太明白我在哪里使用多个流,除非它是读取流和内存流。但是,我也看不到如何关闭使用 file.OpenReadStream 打开的流。

最佳答案

背景:

正确的解决方案:

您正在调用 ImageSharp 的 Image.Load 方法,该方法使用非异步 Stream 方法。解决方法是简单地使用 await Image.LoadAsync 代替:

所以把你的代码改成这样:

// I assume this is a Controller Action method
// This method does not return an IActionResult because it writes directly to the response in the action method. See examples here: https://stackoverflow.com/questions/42771409/how-to-stream-with-asp-net-core

public async Task ResizeImageAsync( IBrowserFile file )
{
await using( Stream stream = file.OpenReadStream() )
using( Image image = await Image.LoadAsync( stream ) )
{
ResizeOptions ro = new ResizeOptions
{
Mode = ResizeMode.Min,
Size = new Size(128)
};

image.Mutate( img => img.Resize( ro ) );

if( file.ContentType == "image/png" ) // <-- You should not do this: *never trust* the client to be correct and truthful about uploaded files' types and contents. In this case it's just images so it's not that big a deal, but always verify independently server-side.
{
this.Response.ContentType = "image/png";
await image.SaveAsPngAsync( this.Response.Body );
}
else
{
this.Response.ContentType = "image/jpeg";
await image.SaveAsJpegAsync( this.Response.Body );
}
}

替代(非)解决方案:拖延:

只需禁用 ASP.NET Core 对非异步 IO 的禁止:

public void ConfigureServices(IServiceCollection services)
{
// If using Kestrel:
services.Configure<KestrelServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});

// If using IIS:
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
}

关于c# - 为什么我得到 ImageSharp 不支持同步读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69056652/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com