(c#.net) front-end JS image compression will get the Base64 string saved as a picture

Source: Internet
Author: User

<Scriptsrc= "Jquery-1.7.2.min.js"></Script><Body>    <inputtype= "File"ID= "img"name= "img" />    <imgID= "Test"src="" />    <inputtype= "button"ID= "Submit"value= "OK"onclick= "Submit ()" />    <Script>                    functionSubmit () {varF=document.getElementById ("img"). files[0]; varReader= NewFileReader (); //Closure to capture the file information.Reader.onload= (function(thefile) {return function(e) {varI=document.getElementById ("Test"); I.SRC=Event.target.result; varData=Compress (i, -); varFD= NewFormData (); Fd.append ("img", data); $.ajax ({URL:"Webform1.aspx?type=upload", type:"POST", DATA:FD, ProcessData:false,  //tell jquery not to process the data sentContentType:false,   //tell jquery not to set the Content-type request headerSuccess:function(data) {$ (". Loadingbox"). Hide (); if(Data== 0) Myalert ("Tips", "Save failed"); ElseMyalerttoone ("Tips", "saved successfully", "share.html");                }                    });            };            }) (f);        Reader.readasdataurl (f); }        functionCompress (source_img_obj, quality, Output_format) {varMime_type= "Image/jpeg"; if(Output_format!=undefined&&Output_format=="PNG") {Mime_type= "Image/png"; }            varCVS=Document.createelement ('Canvas'); //Naturalwidth The width of the real pictureCvs.width=Source_img_obj.naturalwidth; Cvs.height=Source_img_obj.naturalheight; varCTX=Cvs.getcontext ("2d"). DrawImage (Source_img_obj,0, 0); returnCvs.todataurl (mime_type, quality/);        }    </Script></Body>
Front-end code
protected voidPage_Load (Objectsender, EventArgs e) {Base64stringtoimage (request.form["img"]); }        Private voidBase64stringtoimage (stringinputstr) {            if(string. Isnullorwhitespace (INPUTSTR))return; Try            {                stringFilePath ="e:\\bb.jpg"; byte[] arr = convert.frombase64string (inputstr.substring (Inputstr.indexof ("Base64,") +7). Trim (' /')); using(MemoryStream ms =NewMemoryStream (arr)) {Bitmap BMP=NewBitmap (MS); //Create a new BMP2 variable for the second bitmap type. Bitmap BMP2 =NewBitmap (BMP, BMP. Width, BMP.                    Height); //copy the first BMP into the BMP2Graphics draw =graphics.fromimage (BMP2); Draw. DrawImage (BMP,0,0); Draw.                    Dispose (); Bmp2.                    Save (FilePath, System.Drawing.Imaging.ImageFormat.Jpeg); Ms.                Close (); }                           }            Catch(Exception ex) {}}
Back-end code

(c#.net) front-end JS image compression will get the Base64 string saved as a picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.