Support for DecoderOptions/ISpecializedDecoderOptions #322
Answered
by
JimBobSquarePants
hey-red
asked this question in
Show and tell
-
Since 3.0 release there is no way to configure DecoderOptions or other decoders specific options. OnBeforeLoadAsync is unusable in this case. Maybe someone suggest a more elegant way to do this, but I'm did it in simple way: // Add delegate to ImageSharpMiddlewareOptions
private Func<ImageCommandContext, DecoderOptions, Stream, bool, Task<FormattedImage?>> onLoadAsync = (_, _, _, _)
=> Task.FromResult(default(FormattedImage));
public Func<ImageCommandContext, DecoderOptions, Stream, bool, Task<FormattedImage?>> OnLoadAsync
{
get => this.onLoadAsync;
set
{
Guard.NotNull(value, nameof(this.OnLoadAsync));
this.onLoadAsync = value;
}
}
// Change access modifier to public
public FormattedImage(Image image, IImageFormat format, bool keepOpen) {}
// ImageSharpMiddleware.cs
image = await this.options.OnLoadAsync(imageCommandContext, decoderOptions, inStream, requiresAlpha);
if (image is null)
{
if (requiresAlpha)
{
image = await FormattedImage.LoadAsync<Rgba32>(decoderOptions, inStream);
}
else
{
image = await FormattedImage.LoadAsync(decoderOptions, inStream);
}
} and now I have full control on how to image should be decoded: options.OnLoadAsync = async (ctx, decoderOptions, inStream, requiresAlpha) => { }; |
Beta Was this translation helpful? Give feedback.
Answered by
JimBobSquarePants
Apr 5, 2023
Replies: 1 comment 5 replies
-
DAMMIT! I had completely forgotten that the properties on I'll have to change the |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
hey-red
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DAMMIT!
I had completely forgotten that the properties on
DecoderOptions
properties are init only. That makes it impossible to update.I'll have to change the
OnBeforeLoadAsync
method to return a nullable instance of those options.