62,046
社区成员
发帖
与我相关
我的任务
分享
public void MakeThumbnail(string imgPath_old, int width, int height)
{
//这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪求大神提供可以覆盖的方法
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);
int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个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(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
//以jpg格式保存缩略图(这一块保存的时候要报错,因为文件被打开了,不能保存到原来的位置,也就不能覆盖了)
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}
public void MakeThumbnail(string imgPath_old, int width, int height)
{
//这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪
System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imgPath_old)));
int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个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(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
//以jpg格式保存缩略图([color=#FF0000]这一块保存的时候要报错,因为文件被打开了,不能保存到原来的位置,也就不能覆盖了)
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{
MakeThumbnail(Server.MapPath("~/aaa.jpg"), 100, 100);
}
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
调整下顺序
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();