产生高质量成比例缩略图
来源:互联网 发布:老子西出函谷关 知乎 编辑:程序博客网 时间:2024/05/16 04:50
不多说了,大家看代码,生成的缩略图有超出范围的时候,但是在限制范围长和宽为1:1时肯定在范围之内.
private void Get_Image(string imgpath,string thetype,int limit_width,int limit_height) //图片路径,图片形式,范围宽,范围长
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("temp")+"/"+imgpath);
int pic_width = originalImage.Width;
int pic_height = originalImage.Height;
int towidth = 0;
int toheight = 0;
if(pic_width<=limit_width&&pic_height<=limit_height)
{
towidth = pic_width;
toheight = pic_height;
}
else
{
if(pic_width>pic_height)
{
double temp = (double)pic_width/(double)limit_width;
towidth = limit_width;
toheight = (int)(pic_height/temp);
}
else
{
double temp = (double)pic_height/(double)limit_height;
towidth = (int)(pic_width/temp);
toheight = limit_height;
}
}
try
{
System.Drawing.Bitmap bmp;
if(thetype == "full")
{
bmp = new System.Drawing.Bitmap(limit_width,limit_height);
}
else
{
bmp = new System.Drawing.Bitmap(towidth,toheight);
}
Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.White);
if(thetype == "full")
{
int temp = towidth - toheight;
if(temp>0)
{
g.DrawImage(originalImage,new Rectangle(0,temp/2,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
else
{
g.DrawImage(originalImage,new Rectangle(-temp/2,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
}
else
{
g.DrawImage(originalImage,new Rectangle(0,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
originalImage.Dispose();
bmp.Dispose();
g.Dispose();
}
catch
{
}
}
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath("temp")+"/"+imgpath);
int pic_width = originalImage.Width;
int pic_height = originalImage.Height;
int towidth = 0;
int toheight = 0;
if(pic_width<=limit_width&&pic_height<=limit_height)
{
towidth = pic_width;
toheight = pic_height;
}
else
{
if(pic_width>pic_height)
{
double temp = (double)pic_width/(double)limit_width;
towidth = limit_width;
toheight = (int)(pic_height/temp);
}
else
{
double temp = (double)pic_height/(double)limit_height;
towidth = (int)(pic_width/temp);
toheight = limit_height;
}
}
try
{
System.Drawing.Bitmap bmp;
if(thetype == "full")
{
bmp = new System.Drawing.Bitmap(limit_width,limit_height);
}
else
{
bmp = new System.Drawing.Bitmap(towidth,toheight);
}
Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.White);
if(thetype == "full")
{
int temp = towidth - toheight;
if(temp>0)
{
g.DrawImage(originalImage,new Rectangle(0,temp/2,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
else
{
g.DrawImage(originalImage,new Rectangle(-temp/2,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
}
else
{
g.DrawImage(originalImage,new Rectangle(0,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
originalImage.Dispose();
bmp.Dispose();
g.Dispose();
}
catch
{
}
}
- 产生高质量成比例缩略图
- 生成高质量缩略图
- C#高质量缩略图
- 高质量缩略图
- 生成高质量缩略图
- asp.net 等比例生成上传图片的高质量缩略图的方法
- ASP.NET 不经保存,直接生成上传图片的等比例的高质量缩略图
- 高质量缩略图(转载)
- 高质量缩略图(转载)
- c# 生成高质量缩略图
- C#生成高质量缩略图
- C# 生成高质量缩略图
- C# 生成高质量缩略图
- c#生成高质量缩略图
- 生成高质量缩略图通用函数
- 生成高质量缩略图通用函数
- java使用JMagick生成高质量缩略图
- ASP.NET 生成高质量缩略图代码
- C.M. Coolidge画的狗
- 核心(Core) Javascript 学习手记
- Friendly URLs in Tapestry
- 工作日志2006.11.26
- Tomcat Mysql 。。。学习笔记
- 产生高质量成比例缩略图
- 思
- 数字图像处理发展概况(转)
- 图像处理数学方法(转)
- vbscript 中的format函数
- justdo
- 防止ACCESS数据库被下载的9种方法
- 网上找到的关于自定义comboBox Column(datagrid c#)
- 世界上最遥远的距离