Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

c# - Ghostly outline around image after resize -


i'm working on website sell hand made jewelry , i'm finishing image editor, it's not behaving quite right.

basically, user uploads image saved source , resized fit user's screen , saved temp. user go screen allow them crop image , save it's final versions.

all of works fine, except, final versions have 3 bugs. first black horizontal line on bottom of image. second outline of sorts follows edges. thought because reducing quality, @ 100% still shows up... , lastly, i've noticed cropped image couple of pixels lower i'm specifying...

anyway, i'm hoping got experience in editing images c# can maybe take @ code , see might going off right path.

oh, way, in asp.net mvc application.

here's code:

public class imageprovider {     private readonly productprovider productprovider = null;      private readonly encoderparameters highqualityencoder = new encoderparameters();     private readonly imagecodecinfo jpegcodecinfo = imagecodecinfo.getimageencoders().single(         c => (c.mimetype == "image/jpeg"));      private readonly string path = httpcontext.current.server.mappath("~/resources/images/products");     private readonly short[][] dimensions = new short[3][] {         new short[2] { 640, 480 },         new short[2] { 240, 0 },         new short[2] { 80, 60 }     }      public imageprovider(productprovider productprovider) {         this.productprovider = productprovider;          highqualityencoder.param[0] = new encoderparameter(encoder.quality, 100l);     }      public void crop(string filename, image image, crop crop) {         using (bitmap source = new bitmap(image)) {             using (bitmap target = new bitmap(crop.width, crop.height)) {                 using (graphics graphics = graphics.fromimage(target)) {                     graphics.interpolationmode = interpolationmode.highqualitybicubic;                     graphics.smoothingmode = smoothingmode.highquality;                     graphics.pixeloffsetmode = pixeloffsetmode.highquality;                     graphics.compositingquality = compositingquality.highquality;                      graphics.drawimage(source, new rectangle(0, 0, target.width, target.height), new rectangle(crop.left, crop.top, crop.width, crop.height), graphicsunit.pixel);                 }                  target.save(filename, jpegcodecinfo, highqualityencoder);             }         }     }      public void cropandresize(product product, crop crop) {         using (image source = image.fromfile(string.format("{0}/{1}.source", path, product.productid))) {             using (image temp = image.fromfile(string.format("{0}/{1}.temp", path, product.productid))) {                 float percent = ((float)source.width / (float)temp.width);                  short width = (short)(temp.width * percent);                 short height = (short)(temp.height * percent);                  crop.height = (short)(crop.height * percent);                 crop.left = (short)(crop.left * percent);                 crop.top = (short)(crop.top * percent);                 crop.width = (short)(crop.width * percent);                  img img = new img();                  this.productprovider.addimageandsave(product, img);                  this.crop(string.format("{0}/{1}.cropped", path, img.imageid), source, crop);                  using (image cropped = image.fromfile(string.format("{0}/{1}.cropped", path, img.imageid))) {                     this.resize(this.dimensions[0], string.format("{0}/{1}-l.jpg", path, img.imageid), cropped, highqualityencoder);                     this.resize(this.dimensions[1], string.format("{0}/{1}-t.jpg", path, img.imageid), cropped, highqualityencoder);                     this.resize(this.dimensions[2], string.format("{0}/{1}-s.jpg", path, img.imageid), cropped, highqualityencoder);                 }             }         }          this.purge(product);     }      public void queuefor( product product, httppostedfilebase postedfile) {         using (image image = image.fromstream(postedfile.inputstream)) {             this.resize(new short[2] {                 1152,                 0             }, string.format("{0}/{1}.temp", path, product.productid), image, highqualityencoder);         }          postedfile.saveas(string.format("{0}/{1}.source", path, product.productid));     }      private void purge(product product) {         string source = string.format("{0}/{1}.source", path, product.productid);         string temp = string.format("{0}/{1}.temp", path, product.productid);          if (file.exists(source)) {             file.delete(source);         }         if (file.exists(temp)) {             file.delete(temp);         }          foreach (img img in product.imgs) {             string cropped = string.format("{0}/{1}.cropped", path, img.imageid);              if (file.exists(cropped)) {                 file.delete(cropped);             }         }     }      public void resize( short[] dimensions, string filename, image image, encoderparameters encoderparameters) {         if (dimensions[1] == 0) {             dimensions[1] = (short)(image.height / ((float)image.width / (float)dimensions[0]));         }          using (bitmap bitmap = new bitmap(dimensions[0], dimensions[1])) {             using (graphics graphics = graphics.fromimage(bitmap)) {                 graphics.interpolationmode = interpolationmode.highqualitybicubic;                 graphics.smoothingmode = smoothingmode.highquality;                 graphics.pixeloffsetmode = pixeloffsetmode.highquality;                 graphics.compositingquality = compositingquality.highquality;                  graphics.drawimage(image, 0, 0, dimensions[0], dimensions[1]);             };              bitmap.save(filename, jpegcodecinfo, encoderparameters);         }     } } 

here's 1 of images produces:

http://i.stack.imgur.com/gjx97.jpg

update

so, sat down , read through msdn 2 hours after posted, verified code had. far can tell, i'm using highest possible quality settings when performing manipulation.

either way, ended cleaning , streamlining code bit. contemplated need source file , decided remove because requiring me additional work figure out cropping dimensions based on temp file. so, it's gone.

also, somewhere along streamlining, black line disappeared, can assume aspect ratio issues should have been corrected @stefane stated.

also, @vinayc said, re-sizer generating value of 479 height (which still don't how, whatever...), seems have been corrected when switched using system.drawing.size , system.drawing.rectangle classes way through instead of using own classes same thing.

so, here's updated code. remaining two bugs bug still stand, have "ghosting" outline way around image (see second attachment) can narrow down resizing because shows in first re-size no cropping has occurred. and second bug cropped versions positioned lower on y axis pass in cropper. i'd estimate it's 5%-8% lower tell it, not sure 1 (the positions shouldn't changing... unless i'm passing in bad number jquery, gotta check on that...).

(my second bug ended me sending bad values begin with, cropping div getting relative position based on master content div, not image container div. fixed now.)

using system; using system.drawing; using system.drawing.drawing2d; using system.drawing.imaging; using system.io; using system.linq; using system.web;  namespace website.models.providers {     public class imageprovider {         private readonly productprovider productprovider = null;          private readonly encoderparameters defaultqualityencoder = new encoderparameters();         private readonly encoderparameters highqualityencoder = new encoderparameters();         private readonly imagecodecinfo jpegcodecinfo = imagecodecinfo.getimageencoders().single(             c =>                 (c.mimetype == "image/jpeg"));         private readonly size[] sizes = new size[3] {             new size(640, 0),             new size(240, 0),             new size(80, 0)         };          private readonly string path = httpcontext.current.server.mappath("~/resources/images/products");          public imageprovider(             productprovider productprovider) {             this.productprovider = productprovider;              this.defaultqualityencoder.param[0] = new encoderparameter(encoder.quality, 80l);             this.highqualityencoder.param[0] = new encoderparameter(encoder.quality, 100l);         }          public void crop(             string filename,             image image,             crop crop) {             using (bitmap source = new bitmap(image)) {                 using (bitmap target = new bitmap(crop.width, crop.height)) {                     using (graphics graphics = graphics.fromimage(target)) {                         graphics.compositingmode = compositingmode.sourcecopy;                         graphics.compositingquality = compositingquality.highquality;                         graphics.interpolationmode = interpolationmode.highqualitybicubic;                         graphics.pixeloffsetmode = pixeloffsetmode.highquality;                         graphics.smoothingmode = smoothingmode.highquality;                          graphics.drawimage(source, new rectangle(0, 0, target.width, target.height), new rectangle(crop.left, crop.top, crop.width, crop.height), graphicsunit.pixel);                     };                      target.save(filename, jpegcodecinfo, highqualityencoder);                 };             };         }          public void cropandresize(             product product,             crop crop) {             using (image temp = image.fromfile(string.format("{0}/{1}.temp", path, product.productid))) {                 img img = new img();                  this.productprovider.addimageandsave(product, img);                  this.crop(string.format("{0}/{1}.cropped", path, img.imageid), temp, crop);                  using (image cropped = image.fromfile(string.format("{0}/{1}.cropped", path, img.imageid))) {                     this.resize(string.format("{0}/{1}-l.jpg", path, img.imageid), this.sizes[0], cropped, highqualityencoder);                     this.resize(string.format("{0}/{1}-t.jpg", path, img.imageid), this.sizes[1], cropped, highqualityencoder);                     this.resize(string.format("{0}/{1}-s.jpg", path, img.imageid), this.sizes[2], cropped, highqualityencoder);                 };             };              this.purge(product);         }          public void queuefor(             product product,             size size,             httppostedfilebase postedfile) {             using (image image = image.fromstream(postedfile.inputstream)) {                 this.resize(string.format("{0}/{1}.temp", path, product.productid), size, image, highqualityencoder);             };         }          private void purge(             product product) {             string temp = string.format("{0}/{1}.temp", path, product.productid);              if (file.exists(temp)) {                 file.delete(temp);             };              foreach (img img in product.imgs) {                 string cropped = string.format("{0}/{1}.cropped", path, img.imageid);                  if (file.exists(cropped)) {                     file.delete(cropped);                 };             };         }          public void resize(             string filename,             size size,             image image,             encoderparameters encoderparameters) {             if (size.height == 0) {                 size.height = (int)(image.height / ((float)image.width / (float)size.width));             };              using (bitmap bitmap = new bitmap(size.width, size.height)) {                 using (graphics graphics = graphics.fromimage(bitmap)) {                     graphics.compositingmode = compositingmode.sourcecopy;                     graphics.compositingquality = compositingquality.highquality;                     graphics.interpolationmode = interpolationmode.highqualitybicubic;                     graphics.pixeloffsetmode = pixeloffsetmode.highquality;                     graphics.smoothingmode = smoothingmode.highquality;                      graphics.drawimage(image, new rectangle(0, 0, size.width, size.height));                 };                  bitmap.save(filename, jpegcodecinfo, encoderparameters);             };         }     } } 

this image shows "ghosting" outline pointed red arrows.

alt text

regarding black lines in bottom because don't handle aspect ratio.

double aspectratio = imagewidth/imageheight; double boxratio = maxwidth/maxheight; double scalefactor = 0; if (boxratio > aspectratio)  //use height, since restrictive dimension of box.  scalefactor = maxheight / imageheight; else  scalefactor = maxwidth / imagewidth;  double newwidth = imagewidth * scalefactor; double newheight = imageheight * scalefactor; 

source: http://nathanaeljones.com/163/20-image-resizing-pitfalls/


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -