asp.net mvc 3 - Rendering smallest possible image size with MVC3 vs Webforms Library -


i in process of moving webforms app mvc3. ironically enough, cool beans except 1 thing - images served handler, microsoft generated image handler. works - on average 450kb photo gets output @ 20kb.

the actual photo on disk weighs in @ 417kb, getting great reduction.

moving on mvc3 drop handler , use controller action. seem unable achieve same kind of file size reduction when rendering image. walked through source , took exact copy of image transform code yet achieving 230~kb, still lot bigger ms handler outputting - 16kb.

you can see example of both controller , handler here

i have walked through handler source code , cannot see compressing image further. if examine both images can see difference - handler rendered image less clear, more grainy looking, still consider satisfactory needs.

can give me pointers here? output compression somehow being used? or overlooking obvious?

the code below used in home controller render image, , exact copy of fitimage method in image transform class handler uses ...

public actionresult mvcimage()     {         var file = server.mappath("~/content/test.jpg");         var img = system.drawing.image.fromfile(file);         var sizedimg = msscale(img);          var newfile = server.mappath("~/app_data/test.jpg");         if (system.io.file.exists(newfile))         {             system.io.file.delete(newfile);         }         sizedimg.save(newfile);         return file(newfile, "image/jpeg");     }  private image msscale(image img)     {         var scaled_height = 267;         var scaled_width = 400;         int resizewidth = 400;         int resizeheight = 267;         if (img.height == 0)         {             resizewidth = img.width;             resizeheight = scaled_height;         }         else if (img.width == 0)         {             resizewidth = scaled_width;             resizeheight = img.height;         }         else         {             if (((float)img.width / (float)img.width < img.height / (float)img.height))             {                 resizewidth = img.width;                 resizeheight = scaled_height;             }             else             {                 resizewidth = scaled_width;                 resizeheight = img.height;             }         }          bitmap newimage = new bitmap(resizewidth, resizeheight);         graphics gra = graphics.fromimage(newimage);         setupgraphics(gra);         gra.drawimage(img, 0, 0, resizewidth, resizeheight);         return newimage;     }      private void setupgraphics(graphics graphics)     {         graphics.compositingmode = compositingmode.sourcecopy;         graphics.compositingquality = compositingquality.highspeed;         graphics.interpolationmode = interpolationmode.highqualitybicubic;         graphics.smoothingmode = smoothingmode.highspeed;     } 

if don't set quality on encoder, uses 100 default. you'll never size reduction using 100 due way image formats jpeg work. i've got vb.net code example of how set quality parameter should able adapt.

80l here quality setting. 80 still gives high quality image, @ drastic size reduction on 100.

            dim graphic system.drawing.graphics = system.drawing.graphics.fromimage(newimage)             graphic.interpolationmode = drawing.drawing2d.interpolationmode.highqualitybicubic             graphic.smoothingmode = drawing.drawing2d.smoothingmode.highquality             graphic.pixeloffsetmode = drawing.drawing2d.pixeloffsetmode.highquality             graphic.compositingquality = drawing.drawing2d.compositingquality.highquality             graphic.drawimage(sourceimage, 0, 0, width, height)              ' encode , send new image             ' important part              dim info() drawing.imaging.imagecodecinfo = drawing.imaging.imagecodecinfo.getimageencoders()             dim encoderparameters new drawing.imaging.encoderparameters(1)              encoderparameters.param(0) = new drawing.imaging.encoderparameter(drawing.imaging.encoder.quality, 80l)             ms = new system.io.memorystream             newimage.save(ms, info(1), encoderparameters) 

when save or otherwise write image after setting encoder parameters, it'll output using jpeg encoder (in case) set quality 80. size savings you're looking for.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -