0

Where can I find a full sample of an ImageResizer (ImageResizing.net) standalone website based on ASP.NET Core Web Application (.NET Framework) ?

"frameworks": { "net461": { } },

2 Answers 2

1

Here's a working PoC that simulates what ImageResizer + AzureReader2 do.

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ImageResizerSvc
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSingleton(x =>
            {
                var config = new ImageResizer.Configuration.Config();
                // install plugins, e.g.
                // new PrettyGifs().Install(config);
                return config;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute("imageresizer", "{*path}",
                    defaults: new { controller = "Images", action = "Resizer" });
            });
        }
    }
}

ImagesController.cs

using ImageResizer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.WindowsAzure.Storage;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace ImageResizerSvc.Controllers
{
    public class ImagesController : Controller
    {
        private readonly ImageResizer.Configuration.Config _imageResizerConfig;

        public ImagesController(ImageResizer.Configuration.Config imageResizerConfig)
        {
            _imageResizerConfig = imageResizerConfig ?? throw new ArgumentNullException(nameof(imageResizerConfig));
        }

        public async Task<IActionResult> Resizer()
        {
            // Init storage account
            var connectionString = "UseDevelopmentStorage=true";
            CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount cloudStorageAccount);
            var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

            // Get blob ref
            var storagePath = cloudBlobClient.BaseUri.ToString().TrimEnd('/', '\\');
            var blobPath = Request.Path.Value.Trim('/', '\\');
            var blobUri = new Uri($"{storagePath}/{blobPath}");

            using (var sourceStream = new MemoryStream(4096))
            {
                try
                {
                    var blob = await cloudBlobClient.GetBlobReferenceFromServerAsync(blobUri);
                    await blob.DownloadToStreamAsync(sourceStream);
                    sourceStream.Seek(0, SeekOrigin.Begin);
                }
                catch (StorageException e)
                {
                    // Pass to client
                    if (Enum.IsDefined(typeof(HttpStatusCode), e.RequestInformation.HttpStatusCode))
                    {
                        return StatusCode(e.RequestInformation.HttpStatusCode, e.RequestInformation.HttpStatusMessage);
                    }

                    throw;
                }

                var destinationStream = new MemoryStream(4096);

                var instructions = new Instructions(Request.QueryString.Value);
                var imageJob = _imageResizerConfig.Build(new ImageJob(sourceStream, destinationStream, instructions));
                destinationStream.Seek(0, SeekOrigin.Begin);

                return File(destinationStream, imageJob.ResultMimeType);
            }
        }
    }
}

Then you can use it by going to http://localhost/{container}/{blobPath.ext}?{imageResizer_queryString}

Sign up to request clarification or add additional context in comments.

Comments

0

Imageflow.NET Server is the .NET Core equivalent to ImageResizer, but is much faster and produces much smaller image files. See https://github.com/imazen/imageflow-dotnet-server

If you want to do your own middleware, use Imageflow.NET directly. See https://github.com/imazen/imageflow-dotnet

[Disclaimer: I am the author of both ImageResizer and Imageflow]

2 Comments

Thank you Nathanael for this quick answer.
This does not answer what he is asking. How can you get ImageReizer to work with asp.net core running on .net framework?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.