Friday 3 March 2017

asp.net mvc core image handler

It's a while since my last blog , today i am gonna write an asp.net mvc image handler (Middle-ware) with following assumptions:

  1. asp.net mvc core web application based on .net framework/.net core
  2. NuGet package for resize images Magick.NET-Q16-AnyCPU
  3. Image Manager in code used to get Image by id (replace it with your code)
  4. Image will be stored in Database (Binary + MimeType)
  5. Url will be {SiteUrrl}/images?id=xx&h=xx&y=xx
  6. id is image id in database
  7. h image height
  8. w image width


Step 1:Create ImageMiddleware class
Step 2:Create ImageMiddlewareExtensions class
Step 3:Register ImageMiddleware class in startup class



Source Code (Links on GitHub):
ImageMiddleware Class
ImageMiddlewareExtensions


Then in your startup class Add:

 app.UseImageMiddleware("/images");


Testing:
My site url is : http://localhost:14600

http://localhost:14600/images?id=12&w=150&h=150






http://localhost:14600/images?id=12&w=400&h=400