1

How ImageSharp work with Dynamic Images loaded from Database? Here my controller which get image file:

public async Task<FileResult> GetPhoto([FromQuery] GetFileAttachementInputAsync input)
    {
        var file = await filesAttachementAppService
            .GetFileAsync(new GetFileAttachementInputAsync() { FileId = input.FileId })
            .ConfigureAwait(false);
        return file != null 
            ? File(new MemoryStream(file.FileDto.FileContent), file.FileDto.ContentType, file.FileDto.FileName) 
            : null;
    }

And this my Html call:

<img src="/PropertyAdministration/GetPhoto?FileId=@item.MainPhotoId&width=554&height=360" alt="" />

I am using ImageSharp as following:

 public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddImageSharp();
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
    {            
        app.UseImageSharp();
    }

What I am missing here to make this working?

Hanaa Gebril
  • 187
  • 1
  • 13

1 Answers1

1

You're not using the middleware nor the services that provide images to the middleware.

For the middleware to work it needs to be able capture an image request. With the default installation this is done by matching the request to an image source in your physical file system in wwwroot.

In your code though you've created an isolated action result returning a stream containing your image which the middleware has no awareness of.

Disclaimer, the following is based on the latest developer build 1.0.0-dev000131 and though unlikely to change could potentially change before final release.

https://www.myget.org/feed/sixlabors/package/nuget/SixLabors.ImageSharp.Web/1.0.0-dev000131

In order to provide images from a custom source you will need to create your own implementation of the IImageProvider and IImageResolver you can use examples in the source to base your implementation from.

Once implemented you will need to register the implementations via dependency injection. This needs to use a more fine grained registration since you are no longer using the defaults.

// Fine-grain control adding the default options and configure all other services. Setting all services is required.
services.AddImageSharpCore()
        .SetRequestParser<QueryCollectionRequestParser>()
        .SetBufferManager<PooledBufferManager>()
        .SetMemoryAllocatorFromMiddlewareOptions()
        .SetCacheHash<CacheHash>()
        .AddProvider<PhysicalFileSystemProvider>()
        /// Add your provider here via AddProvider<T>().
        .AddProvider<PhysicalFileSystemProvider>()
        .AddProcessor<ResizeWebProcessor>()
        .AddProcessor<FormatWebProcessor>()
        .AddProcessor<BackgroundColorWebProcessor>();

You should then be able to remove your action result completely and use the IImageProvider and IImageResolver combination to identify the request and return the image.

James South
  • 10,147
  • 4
  • 59
  • 115