gdi+ - ASP.NET convert image from PNG to jpg -
i having problem converting image png jpg. jpg jpg png jpg having pixelated problem. i've implemented compression method code still couldn't produce high quality image.
any ideas guys?
using system; using system.collections.generic; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.drawing.imaging; using system.drawing; using system.io; using system.drawing.drawing2d; public partial class _default : system.web.ui.page { private imagecodecinfo getencoder(imageformat format) { imagecodecinfo[] codecs = imagecodecinfo.getimagedecoders(); foreach (imagecodecinfo codec in codecs) { if (codec.formatid == format.guid) { return codec; } } return null; } protected void page_load(object sender, eventargs e) { response.contenttype = "image/jpeg";//convert tojpeg web usage // bitmap. bitmap bmp1 = new bitmap(@"e:\websites\logomaster\logoid4.png"); imagecodecinfo jgpencoder = getencoder(imageformat.jpeg); // create encoder object based on guid // quality parameter category. system.drawing.imaging.encoder myencoder =system.drawing.imaging.encoder.quality; // create encoderparameters object. // encoderparameters object has array of encoderparameter // objects. in case, there 1 // encoderparameter object in array. encoderparameters myencoderparameters = new encoderparameters(1); encoderparameter myencoderparameter = new encoderparameter(myencoder, 100l);//set highest quality myencoderparameters.param[0] = myencoderparameter; // bmp1.save(@"e:\websites\logomaster\testphotoqualityhundredp.jpg", jgpencoder, myencoderparameters); bmp1.save(response.outputstream, jgpencoder, myencoderparameters); bmp1.dispose(); } }
maybe need redraw image? answered question on generating thumbnails (asp.net version of timthumb php class) made images nice , clean.
dim bmpthumb new bitmap(destwidth, destheight) dim g = graphics.fromimage(bmpthumb) g.interpolationmode = drawing2d.interpolationmode.highqualitybicubic g.compositingquality = drawing2d.compositingquality.highquality g.smoothingmode = drawing2d.smoothingmode.antialias g.drawimage(img, _ new rectangle(0, 0, destwidth, destheight), _ new rectangle(sourcex, sourcey, sourcewidth, sourceheight), graphicsunit.pixel) '-----write out thumbnail output stream------' 'get jpeg image coded info can use when saving' dim ici imaging.imagecodecinfo = imaging.imagecodecinfo.getimageencoders().where(function(c) c.mimetype = "image/jpeg").first() 'save image memory stream' dim ms new memorystream() bmpthumb.save(ms, ici, buildqualityparams(context))
once save memory stream need write out asp.net response stream:
context.response.clear() context.response.contenttype = "image/jpeg" ms.writeto(context.response.outputstream) context.response.flush()
Comments
Post a Comment