<summary>///Compressed picture///</summary>//<param name= "ISource" > Picture file </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> ; <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