Reylin Mathew

Reylin Mathew

  • NA
  • 347
  • 75.3k

resize crop and image

Apr 5 2018 9:51 AM
I have a system here to upload an image and crop it using asp.net. When I save the cropped image, It won't resize and stay as the original. How do I resize the picture?
This is my code.
 
Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="resizepic.aspx.cs" Inherits="WebApplication.resizepic" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. <link href="css/jquery.Jcrop.css" rel="stylesheet" type="text/css" />  
  7. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>  
  8. <script src="script/jquery.Jcrop.js" type="text/javascript"></script>  
  9. <script type="text/javascript" >  
  10. $(window).load(function () {  
  11. var jcrop_api;  
  12. var i, ac;  
  13. initJcrop();  
  14. function initJcrop() {  
  15. jcrop_api = $.Jcrop('#imgCrop', {  
  16. onSelect: storeCoords,  
  17. onChange: storeCoords//, onSelect: storeCoords(c)  
  18. });  
  19. jcrop_api.setOptions({ aspectRatio: 1 / 1 });  
  20. jcrop_api.setOptions({  
  21. minSize: [250, 250],  
  22. maxSize: [250, 350]  
  23. //,onSelect: storeCoords(c)  
  24. //onChange: SetCoordinates,  
  25. // onSelect: SetCoordinates  
  26. });  
  27. jcrop_api.setSelect([140, 180, 160, 180]);  
  28. };  
  29. function storeCoords(c) {  
  30. jQuery('#X').val(c.x);  
  31. jQuery('#Y').val(c.y);  
  32. jQuery('#W').val(c.w);  
  33. jQuery('#H').val(c.h);  
  34. };  
  35. });  
  36. </script>  
  37. </head>  
  38. <body>  
  39. <form id="form1" runat="server">  
  40. <div>  
  41. <asp:Panel ID="pnlUpload" runat="server">  
  42. <asp:FileUpload ID="Upload" runat="server" />  
  43. <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />  
  44. <asp:Label ID="lblError" runat="server" Visible="false" />  
  45. </asp:Panel>  
  46. <asp:Panel ID="pnlCrop" runat="server" Visible="false" style="width:250px;height:auto" >  
  47. <asp:Image ID="imgCrop" runat="server" />  
  48. <asp:HiddenField ID="X" runat="server" />  
  49. <asp:HiddenField ID="Y" runat="server" />  
  50. <asp:HiddenField ID="W" runat="server" />  
  51. <asp:HiddenField ID="H" runat="server" />  
  52. </asp:Panel>  
  53. <asp:Panel ID="pnlCropped" runat="server" Visible="false">  
  54. <asp:Image ID="imgCropped" runat="server" />  
  55. </asp:Panel>  
  56. <asp:Button ID="btnCrop" runat="server" Text="Crop" OnClick="btnCrop_Click" />  
  57. </div>  
  58. </form>  
  59. </body>  
  60. </html>  
Default.aspx.cs
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. }  
  4. protected void btnUpload_Click(object sender, EventArgs e)  
  5. {  
  6. Boolean FileOK = false;  
  7. Boolean FileSaved = false;  
  8. if (Upload.HasFile)  
  9. {  
  10. Session["UploadImage"] = Upload.FileName;  
  11. String ExtensionofImage = Path.GetExtension(Session["UploadImage"].ToString()).ToLower();  
  12. String[] allowed = { ".png"".jpeg"".jpg"".gif" };  
  13. for (int i = 0; i < allowed.Length; i++)  
  14. {  
  15. if (ExtensionofImage == allowed[i])  
  16. {  
  17. FileOK = true;  
  18. }  
  19. }  
  20. }  
  21. if (FileOK)  
  22. {  
  23. try  
  24. {  
  25. Upload.PostedFile.SaveAs(path + Session["UploadImage"]);  
  26. FileSaved = true;  
  27. }  
  28. catch (Exception ex)  
  29. {  
  30. lblError.Text = "Sorry Can't Upload" + ex.Message.ToString();  
  31. lblError.Visible = true;  
  32. FileSaved = false;  
  33. }  
  34. }  
  35. else  
  36. {  
  37. lblError.Text = "Select some Other Image";  
  38. lblError.Visible = true;  
  39. }  
  40. if (FileSaved)  
  41. {  
  42. pnlUpload.Visible = false;  
  43. pnlCrop.Visible = true;  
  44. imgCrop.ImageUrl = "images/" + Session["UploadImage"].ToString();  
  45. }  
  46. }  
  47. protected void btnCrop_Click(object sender, EventArgs e)  
  48. {  
  49. string Img = Session["UploadImage"].ToString();  
  50. int w = Convert.ToInt32(W.Value);  
  51. int h = Convert.ToInt32(H.Value);  
  52. int x = Convert.ToInt32(X.Value);  
  53. int y = Convert.ToInt32(Y.Value);  
  54. byte[] CropImage = Cut(path + Img, w, h, x, y);  
  55. using (MemoryStream mem = new MemoryStream(CropImage, 0, CropImage.Length))  
  56. {  
  57. mem.Write(CropImage, 0, CropImage.Length);  
  58. using (SD.Image CroppedImage = SD.Image.FromStream(mem, true))  
  59. {  
  60. string SaveTo = path + "crop" + Img;  
  61. CroppedImage.Save(SaveTo, CroppedImage.RawFormat);  
  62. pnlCrop.Visible = false;  
  63. pnlCropped.Visible = true;  
  64. imgCropped.ImageUrl = "images/crop" + Img;  
  65. }  
  66. }  
  67. }  
  68. static byte[] Cut(string Img, int Breadth, int Length, int X, int Y)  
  69. {  
  70. try  
  71. {  
  72. using (SD.Image OriginalImage = SD.Image.FromFile(Img))  
  73. {  
  74. using (SD.Bitmap bmp = new SD.Bitmap(Breadth, Length))  
  75. {  
  76. bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);  
  77. using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))  
  78. {  
  79. Graphic.SmoothingMode = SmoothingMode.AntiAlias;  
  80. Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  81. Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;  
  82. Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Breadth, Length), X, Y, Breadth, Length, SD.GraphicsUnit.Pixel);  
  83. MemoryStream ms = new MemoryStream();  
  84. bmp.Save(ms, OriginalImage.RawFormat);  
  85. return ms.GetBuffer();  
  86. }  
  87. }  
  88. }  
  89. }  
  90. catch (Exception Ex)  
  91. {  
  92. throw (Ex);  
  93. }  
  94. }  
plz help me..

Answers (1)