asp.net - Passing in a HttpPostedFileWrapper variable to a function breaks another function. Image validation and conversion in C# -


so part of program editing/uploading new profile picture user's account. previously, worked fine. decided add in picture validations (picture has have dimensions, etc). made separate helper class that takes in httppostedfilewrapper variable initialized in controller.

so, in controller function, initialize new instance of validateimage class holds 2 functions (dovalidation , resize).

the resize function working fine until added dovalidation function , feel has memory stream.

i "invalid parameter" error in resizeimage function (see below), though never changed code , working fine previously. have filestream not being closed or something?

here code:

 //controller.cs  public virtual actionresult editmyprofilepicture(bool? ignore) {     var loggedinemployee = this.employeerepos.getemployeebyusername(user.identity.name);     int tgtwidth = 250, tgtheight = 250;      try     {         // reference posted file         var file = request.files["filecontent"] httppostedfilewrapper;         validateimage img = new validateimage();          if (file != null && file.contentlength > 0)         {             // isolate filename - ie returns full local path, other browsers: file name.             int index = file.filename.lastindexof("\\");             // if not ie, index -1, -1 + 1 = 0 okay.               string filename = file.filename.substring(index + 1);              // validate image             img.dovalidation(file, tgtwidth, tgtheight);             if (!img.isvalidated)             {                 throw new argumentexception(img.message);             }             else             {                 byte[] resizedimg = img.resize(file, tgtwidth, tgtheight);                 this.employeerepos.saveprofileimage(loggedinemployee.employeecode, resizedimg);             }              return redirecttoaction(mvc.employees.editmyprofile());         }         else         {             throw new argumentexception("please select file upload.");         }     }     catch (exception ex)     {         modelstate.addmodelerror(string.empty, ex.message);     }      return view(views.editmyprofilepicture, loggedinemployee); }  // validateimage.cs  public class validateimage {     public string message { get; private set; }     public bool isvalidated { get; private set; }      public void dovalidation(httppostedfilewrapper file, int tgtwidth, int tgtheight)     {         try         {             image img = image.fromstream(file.inputstream);              int curheight = img.height, curwidth = img.width;              // check image small             if (curheight < tgtheight || curwidth < tgtwidth)             {                 message = "image small. please upload picture @ least 250x250.";                 isvalidated = false;                 return;             }             // check image square             else if (curheight != curwidth)             {                 message = "image not square.";                 isvalidated = false;                 return;             }             else             {                 isvalidated = true;             }         }         catch         {          }      }      public byte[] resize(httppostedfilewrapper file, int tgtwidth, int tgtheight)     {         byte[] bytes = new byte[file.contentlength];         file.inputstream.read(bytes, 0, file.contentlength);         file.inputstream.close(); // close file stream.           // down-sample if needed current byte array max 250x250 jpeg         byte[] resized = helpers.imageresizer.resizeimage(bytes, tgtwidth, tgtheight, resizeoptions.maxwidthandheight, imageformat.jpeg);         return resized;     } }  // resize image function  public static byte[] resizeimage(byte[] bytes, int width, int height, resizeoptions resizeoptions, imageformat imageformat) {     using (memorystream ms = new memorystream(bytes))     {         image img = image.fromstream(ms);         bitmap bmp = new bitmap(img);         bmp = resizeimage(bmp, width, height, resizeoptions);         bmp.setresolution(72, 72);         bmp.save(ms, imageformat);          return ms.toarray();     } } 


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -