Kumar Ravishankar

Kumar Ravishankar

  • NA
  • 437
  • 7.9k

crop 4-5 image,before saving in DB,C#,JQuery

Feb 6 2018 11:22 PM
I have to be upload 4-5 image, and have to upload all them , if size is larger then I have to crop all then then save details in db.if I have to upload one image and crop then its fine but getting issue with more than one Image.
  1. public void ProcessRequest(HttpContext context)  
  2. {  
  3. Boolean FileOK = false;  
  4. Boolean SizeOK = false;  
  5. try  
  6. {  
  7. if (context.Request.Files.Count > 0)  
  8. {  
  9. BEL_Package objBel = new BEL_Package();  
  10. string Output = string.Empty;  
  11. HttpFileCollection files = context.Request.Files;  
  12. for (int i = 0; i < files.Count; i++)  
  13. {  
  14. HttpPostedFile file = files[i];  
  15. string sImageName = file.FileName;  
  16. file.SaveAs(HttpContext.Current.Server.MapPath("~/" + Path.GetFileName(file.FileName)));  
  17. System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(HttpContext.Current.Server.MapPath("~/" + Path.GetFileName(file.FileName)));  
  18. int iwidth = bitmap.Width;  
  19. int iheight = bitmap.Height;  
  20. bitmap.Dispose();  
  21. //Info.InnerHtml = "Original Image ";  
  22. //Info.InnerHtml = Info.InnerHtml + "  
  23. Width: " + iwidth + ", Height: " + iheight +  
  24. // "  
  25. " + hpf.ContentLength / 1024 + " KB   
  26. ";  
  27. // ONCE WE GOT ALL THE INFORMATION, WE'll NOW PROCESS IT.  
  28. // CREATE AN IMAGE OBJECT USING ORIGINAL WIDTH AND HEIGHT.  
  29. // ALSO DEFINE A PIXEL FORMAT (FOR RICH RGB COLOR).  
  30. System.Drawing.Image objOptImage = new System.Drawing.Bitmap(iwidth, iheight, System.Drawing.Imaging.  
  31. PixelFormat.Format16bppRgb555);  
  32. // GET THE ORIGINAL IMAGE.  
  33. using (System.Drawing.Image objImg =  
  34. System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/" + sImageName)))  
  35. {  
  36. // RE-DRAW THE IMAGE USING THE NEWLY OBTAINED PIXEL FORMAT.  
  37. using (System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(objOptImage))  
  38. {  
  39. var _1 = oGraphic;  
  40. System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, iwidth, iheight);  
  41. _1.DrawImage(objImg, oRectangle);  
  42. }  
  43. // SAVE THE OPTIMIZED IMAGE.  
  44. objOptImage.Save(HttpContext.Current.Server.MapPath("~/images/" + sImageName), System.Drawing.Imaging.ImageFormat.Png);  
  45. objImg.Dispose();  
  46. }  
  47. objOptImage.Dispose();  
  48. // FINALLY SHOW THE OPTIMIZED IMAGE DETAILS WITH SIZE.  
  49. System.Drawing.Bitmap bitmap_Opt = new System.Drawing.Bitmap(HttpContext.Current.Server.MapPath("~/images/" + Path.GetFileName(sImageName)));  
  50. int iwidth_Opt = bitmap_Opt.Width;  
  51. int iheight_Opt = bitmap_Opt.Height;  
  52. bitmap_Opt.Dispose();  
  53. // FOR SIZE.  
  54. FileInfo OptImgInfo = new FileInfo(HttpContext.Current.Server.MapPath("~/images/" + Path.GetFileName(sImageName)));  
  55. long lFileSize = OptImgInfo.Length; // GET THE SIZE IN BYTES.  
  56. string fname;  
  57. if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")  
  58. {  
  59. string[] testfiles = file.FileName.Split(new char[] { '\\' });  
  60. fname = testfiles[testfiles.Length - 1];  
  61. }  
  62. else  
  63. {  
  64. fname = file.FileName;  
  65. }  
  66. string path = context.Server.MapPath("~/Admin/AllPackageImages/");  
  67. fname = Path.Combine(path, fname);  
  68. string PackageId = HttpContext.Current.Request.Form["PackageId"];  
  69. int j;  
  70. bool result = int.TryParse(PackageId, out j);  
  71. if (result == false)  
  72. {  
  73. j = GeticonsId(PackageId);  
  74. }  
  75. else  
  76. {  
  77. j = Convert.ToInt32(PackageId);  
  78. }  
  79. objBel.filename = file.FileName; ;  
  80. objBel.P_ID_N = Convert.ToInt32(j);  
  81. objBel.Mode = "InsertBanner";  
  82. objBel.R_ID_N = i;  
  83. BAL_PackageINBandInternational objBalII = new BAL_PackageINBandInternational();  
  84. Output = objBalII.Insert_BannerImages(objBel);  
  85. if (Output.ToLower().Contains("successfully"))  
  86. {  
  87. file.SaveAs(fname);  
  88. context.Response.ContentType = "text/plain";  
  89. context.Response.Write("File Uploaded Successfully!");  
  90. }  
  91. else  
  92. {  
  93. context.Response.ContentType = "text/plain";  
  94. context.Response.Write("Something wrong happened . Pleasde try again!");  
  95. break;  
  96. }  
  97. }  
  98. HttpContext.Current.Response.Redirect("Admin/CropUserProfile.aspx");  
  99. }  
  100. }  
  101. catch (Exception ex)  
  102. {  
  103. context.Response.ContentType = "text/plain";  
  104. context.Response.Write("Something wrong happened . Please try again!");  
  105. }  
  106. }