1

I have an image handler which serves jpegs from disk to the browser, like this:

_context.Response.ContentType = "image/jpeg";
_context.Response.TransmitFile(filepath);

Trying to follow Googles pagespeed recommendations and it's advising me to losslessly compress these images.

Is there a way I can do that within this handler before serving them. There are a few hundred thousand so optimising them individually wouldn't really be an option.

Thanks

Ben
  • 609
  • 6
  • 21
  • 1
    Google is recommending you to *losslessly compress* JPEG files? Apparently Google isn't what it used to be. – Frédéric Hamidi Feb 10 '16 at 15:55
  • Indeed - Losslessly compressing https://***.uk/image.ashx?Id=62039&width=402&height=288&type=Car could save 1.4KiB (10% reduction). – Ben Feb 10 '16 at 15:58
  • Not sure it would be worth it (and not sure you would get 10% reduction on all JPEG images). Seems like YSlow also recommends compressing images. [Like others, I don't think this is a good idea](http://stackoverflow.com/q/4957172/464709). – Frédéric Hamidi Feb 10 '16 at 16:05
  • There are [many image optimization hints](https://developers.google.com/speed/pagespeed/service/OptimizeImages). In order to help you properly analyze the message, you need a two-step approach: first, determine what optimization Google is actually suggesting, then determine how to do that in ASP.NET. – CodeCaster Feb 10 '16 at 16:20

1 Answers1

1

Put this into your Web.config to enable compression

<system.webServer>
  <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

However mind that dynamic compression has a performance hit, so do your benchmarking first ;)

wertzui
  • 5,148
  • 3
  • 31
  • 51