Stream st = File1.PostedFile.InputStream;
System.Drawing.Bitmap map = new Bitmap(st);
//调整图片的长短比
double dl = 1;
//设置最大长度
double maxlength = 300;
if (map.Width >= map.Height)
{
dl = (double)(maxlength / (double)map.Width);//求比例 }
else
{
dl = (double)(maxlength / (double)map.Height);
}
//构建新的图片
System.Drawing.Bitmap newmap = new Bitmap(map, (int)(map.Width * dl), (int)(map.Height * dl));
//Response.Clear();
//图片保存 webFilePath为图片保存路径
newmap.Save(webFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放内存
map.Dispose();
newmap.Dispose();
//=========================
Session["path"] = webFilePath; System.Drawing.Image uploadImage = System.Drawing.Image.FromFile(webFilePath);
UpImg.PhotoMode modes = (uploadImage.Width > uploadImage.Height ? UpImg.PhotoMode.W : UpImg.PhotoMode.H);//判断图片宽与高的长度,选择合适的压缩方式
UpImg.MakeThumbnail(webFilePath, webFilePath_s, 130, 130, modes); // 生成缩略图方法 /**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, PhotoMode mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case PhotoMode.HW:
break;
case PhotoMode.W:
toheight = originalImage.Height * width / originalImage.Width;
break;
case PhotoMode.H:
towidth = originalImage.Width * height / originalImage.Height;
break;
case PhotoMode.Cut:
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
System.Drawing.Bitmap map = new Bitmap(st);
//调整图片的长短比
double dl = 1;
//设置最大长度
double maxlength = 300;
if (map.Width >= map.Height)
{
dl = (double)(maxlength / (double)map.Width);//求比例 }
else
{
dl = (double)(maxlength / (double)map.Height);
}
//构建新的图片
System.Drawing.Bitmap newmap = new Bitmap(map, (int)(map.Width * dl), (int)(map.Height * dl));
//Response.Clear();
//图片保存 webFilePath为图片保存路径
newmap.Save(webFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放内存
map.Dispose();
newmap.Dispose();
//=========================
Session["path"] = webFilePath; System.Drawing.Image uploadImage = System.Drawing.Image.FromFile(webFilePath);
UpImg.PhotoMode modes = (uploadImage.Width > uploadImage.Height ? UpImg.PhotoMode.W : UpImg.PhotoMode.H);//判断图片宽与高的长度,选择合适的压缩方式
UpImg.MakeThumbnail(webFilePath, webFilePath_s, 130, 130, modes); // 生成缩略图方法 /**/
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, PhotoMode mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case PhotoMode.HW:
break;
case PhotoMode.W:
toheight = originalImage.Height * width / originalImage.Width;
break;
case PhotoMode.H:
towidth = originalImage.Width * height / originalImage.Height;
break;
case PhotoMode.Cut:
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
但是如果,我在上传的代码后面加个File.Delete(路径),把其它代码都屏掉,这个文件是可以删掉的,,其它机子也可以, 是不是生成缩略图里出了问题啊?或者什么资源什么的没释放,我看来看去都已经释放了呀。。 很疑惑。。大家来帮忙看下呀
System.Drawing.Image uploadImage = System.Drawing.Image.FromFile(webFilePath);
UpImg.PhotoMode modes = (uploadImage.Width > uploadImage.Height ? UpImg.PhotoMode.W : UpImg.PhotoMode.H);//判断图片宽与高的长度,选择合适的压缩方式//拿到图片大小后释放图片资源
uploadImage.Dispose();
uploadImage = null;UpImg.MakeThumbnail(webFilePath, webFilePath_s, 130, 130, modes); // 生成缩略图方法