C#图片处理 缩略图 文件流返回

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;
}

  

posted @ 2020-05-21 10:24  上帝视角  阅读(655)  评论(0编辑  收藏  举报