Problem with reducing the size of images before sending them to the server?

Viewed 26

In a part of our site, we need to upload a photo, and we want the zero-kilometer user to be involved in shrinking the photo, and these things do not work. The speed of sending to the server should be increased, otherwise if I want to reduce the volume on the server side, there is no problem

        <asp:requiredfieldvalidator controltovalidate="FileUpload1" runat="server" id="RequiredFieldValidator6" errormessage="" validationgroup="j1" display="Dynamic" text="" setfocusonerror="True"></asp:requiredfieldvalidator>
        <input type="button" value="برای انتخاب دوبار کلیک کنید"  onclick="ResizeImage()" />
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <img width="200" height="200" src="" runat="server" id="output">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick" />
           <script type="text/javascript">
               function ResizeImage() {
                   if (window.File && window.FileReader && window.FileList && window.Blob) {
                       var file =  document.getElementById('<%=FileUpload1.ClientID%>').files[0];
                       if (file) {
                           var reader = new FileReader();
                           // Set the image once loaded into file reader
                           reader.onload = function (e) {
                               var img = document.createElement("img");
                               img.src = e.target.result;
                               var canvas = document.createElement("canvas");
                               var ctx = canvas.getContext("2d");                              ctx.drawImage(img, 0, 0);
                               var MAX_WIDTH = 300;
                               var MAX_HEIGHT = 300;
                               var width = img.width;
                              var height = img.height;
                              if (width > height) {                                  if (width > MAX_WIDTH) {                                      height *= MAX_WIDTH / width;                                      width = MAX_WIDTH;
                                   }                               } else {                                   if (height > MAX_HEIGHT) {                                       width *= MAX_HEIGHT / height;                                       height = MAX_HEIGHT;
                                   }
                               }                              canvas.width = width;                               canvas.height = height;
                               var ctx = canvas.getContext("2d");                               ctx.drawImage(img, 0, 0, width, height);
                               dataurl = canvas.toDataURL(file.type);                               document.getElementById('<% =HiddenField1.ClientID%>').value= document.getElementById('output').src = dataurl;
                           }                       reader.readAsDataURL(file);
                       }
                   } else {
                       alert('مرورگر شما چنین کدی را ساپورت نمی کند.');
                   }
               }
           </script>
int whith=300;
     int height = 300;
     byte[] image1;
     try
     {
        image1 = Convert.FromBase64String(HiddenField1.Value.Replac  e("data:image/jpeg;base64,", ""));
     }
     catch
     {
          image1 = Convert.FromBase64String(HiddenField1.Value.Replac  e("data:image/png;base64,", ""));
     }
     using (MemoryStream mem = new MemoryStream(image1))
     {
         Stream stream = mem;
         Bitmap image = new Bitmap(stream);
         Bitmap target = new Bitmap(width, height);
         Graphics graphic = Graphics.FromImage(target);
         graphic.DrawImage(image, 0, 0, width, height);         target.Save(Server.MapPath(@"~/up/" + FileUpload1.FileName));
     }
0 Answers
Related