C # picture Compression upload

Source: Internet
Author: User

   <summary>///Compressed picture///</summary>//<param name= "ISource" > Picture file &LT;/PARAM&G        T <param name= "Outpath" > Path </param> such as var Outpath = Path.Combine (Request.mappath ("~/upload"), Path.getfilename (file.        FileName));        <param name= "Flag" > the larger the value, the better the picture quality, the general default control at 50 for best compression quality </param>//<returns></returns> public static bool Reduceimage (Image ISource, string outpath, int flag) {ImageFormat Tformat = ISource.            Rawformat;            EncoderParameters EP = new EncoderParameters ();            long[] qy = new Long[1];            QY[0] = flag;            Encoderparameter Eparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, QY); Ep.            Param[0] = Eparam;                try {imagecodecinfo[] Arrayici = Imagecodecinfo.getimagedecoders ();                ImageCodecInfo jpegiciinfo = null; for (int x = 0; x < arrayici.lengtH X + +) {if (arrayici[x].                        Formatdescription.equals ("JPEG")) {jpegiciinfo = arrayici[x];                    Break                }} if (Jpegiciinfo! = null) Isource.save (Outpath, Jpegiciinfo, EP);                else Isource.save (Outpath, Tformat);            return true;            } catch {return false;            } finally {isource.dispose (); }        }

Picture type conversion

<summary>        ///byte[] Picture conversion///        </summary>/        <param name= "Buffer" ></param>        //<returns></returns> public static Image bytestoimage (byte[] buffer)        {            MemoryStream ms = new MemoryStream (buffer);            Image image = System.Drawing.Image.FromStream (ms);            return image;        

  

  <summary>////Use stream mode to upload and save files///</summary>//<param name= "s" > File stream </param&        Gt <param name= "Savepath" > Save file path </param>//<param name= "Warning" > Process warning message, if empty, the processing succeeds </param&gt        ; <returns> return file Save full path </returns> public static string Uploadbystream (Stream s, String savepathfolder,                Out string warning) {try {int is = 0;                    if (s.length > 0) {MemoryStream ms = new MemoryStream (); while (is = S.readbyte ())! =-1) {Ms.                    WriteByte ((byte) be); } string reletivefolder = "uploadfile\\manageimage\\" + savepathfolder + "\ \" + DateTime.Now.Year + "\ \                    "+ DateTime.Now.Month;                    String folderpath = HttpContext.Current.Server.MapPath ("~/") + Reletivefolder; if (! Directory.exIsts (FolderPath)) directory.createdirectory (FolderPath);                    string filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". jpg";                    String NewPath = FolderPath + "\ \" + filename;                    String lastpath = "";                    Lastpath = NewPath;                    Fileexistmappath (NewPath, FILECHECKMODEL.M, out Lastpath);                    FileStream fs = new FileStream (Lastpath, FileMode.Create); Ms.                    WriteTo (FS); Release Ms.                    Dispose (); Fs.                    Dispose ();                    Warning = "";                return reletivefolder + "\ \" + filename;                } warning = "error";            Return ""; } catch (Exception ex) {warning = "error" + ex.                Message;            Return ""; }        }

  

C # picture Compression upload

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.