private Stream GetThumbnailImage(string imgPath, int widht = 50, int height = 50) { try { if (string.IsNullOrEmpty(imgPath)) { //imgPath = ConfigSettings.CurrentPath + "App_Data\Img\APPDefault.jpg"; return null; } else { string folder = ConfigSettings.UploadFilePath.Replace("/", "\"); string basepath = GetUploadPath() + folder + ""; if (!File.Exists(imgPath)) { imgPath = ConfigSettings.CurrentPath + "App_Data\Img\APPDefault.jpg"; } } //EncoderParameters ep = new EncoderParameters(); //long[] qy = new long[1]; //qy[0] = flag;//设置压缩的比例1-100 //EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); //ep.Param[0] = eParam; byte[] photoBytes = File.ReadAllBytes(imgPath); ISupportedImageFormat format = new JpegFormat { Quality = 20 }; Size size = new Size(widht, height); ResizeLayer resizeLayer = new ResizeLayer(size, ResizeMode.Min, AnchorPosition.Center); using (MemoryStream inStream = new MemoryStream(photoBytes)) { MemoryStream outStream = new MemoryStream(); using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { imageFactory.Load(inStream) .Resize(resizeLayer) .Format(format) .Save(outStream); } WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";//可以换成其它格式的图片 return outStream; } } catch (Exception ex) { WriteExceptionLog(ex); } return null; }