/// <summary>
/// 把文件流中的图片按指定的大小保存到指定的文件中
/// </summary>
/// <param name="imagestream">保存图片的文件流</param>
/// <param name="compresspath">压缩图保存地址</param>
/// <param name="width">压缩后图片的宽度</param>
/// <param name="height">压缩后图片的高度</param>
/// <returns>保存是否成功</returns>
/// 一般错误:到保存图片时出现GDI错误——一般为保存图片的地址不正确或地址不存在
protected bool SaveCompress(Stream imagestream, string compresspath, int width, int height)
{
bool issuccess = false;
Regex r = new Regex("ImageFormat:(?<content>([^\\]]*))");
System.Drawing.Image image = System.Drawing.Image.FromStream(imagestream);//获取源图
//判断保存图片的文件夹是否存在,若不存在则创建
string filepath = compresspath.Substring(0, compresspath.LastIndexOf("\\"));
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
System.Drawing.Image simage = new System.Drawing.Bitmap(width, height);//新建一张图片
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(simage);//新建一张画布
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);//清空画布以透明填充
g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
try
{
simage.Save(compresspath, image.RawFormat);//保存压缩图
issuccess = true;
log.Warn("保存简图成功!");
}
catch (Exception e)
{
Directory.Delete(filepath);
log.Error("保存简图错误:" + e.Message);
}
finally
{
//释放资源
image.Dispose();
simage.Dispose();
g.Dispose();
imagestream.Dispose();
log.Warn("保存简图:简图地址:" + compresspath);
}
return issuccess;
}
/// 把文件流中的图片按指定的大小保存到指定的文件中
/// </summary>
/// <param name="imagestream">保存图片的文件流</param>
/// <param name="compresspath">压缩图保存地址</param>
/// <param name="width">压缩后图片的宽度</param>
/// <param name="height">压缩后图片的高度</param>
/// <returns>保存是否成功</returns>
/// 一般错误:到保存图片时出现GDI错误——一般为保存图片的地址不正确或地址不存在
protected bool SaveCompress(Stream imagestream, string compresspath, int width, int height)
{
bool issuccess = false;
Regex r = new Regex("ImageFormat:(?<content>([^\\]]*))");
System.Drawing.Image image = System.Drawing.Image.FromStream(imagestream);//获取源图
//判断保存图片的文件夹是否存在,若不存在则创建
string filepath = compresspath.Substring(0, compresspath.LastIndexOf("\\"));
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
System.Drawing.Image simage = new System.Drawing.Bitmap(width, height);//新建一张图片
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(simage);//新建一张画布
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);//清空画布以透明填充
g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
try
{
simage.Save(compresspath, image.RawFormat);//保存压缩图
issuccess = true;
log.Warn("保存简图成功!");
}
catch (Exception e)
{
Directory.Delete(filepath);
log.Error("保存简图错误:" + e.Message);
}
finally
{
//释放资源
image.Dispose();
simage.Dispose();
g.Dispose();
imagestream.Dispose();
log.Warn("保存简图:简图地址:" + compresspath);
}
return issuccess;
}