上传缩略图如何实现不变形?等比例缩放

postcha 2012-03-27 08:25:29
目前上传图片和生成缩略图的代码:

//这是标题图片上传方法
if (image != null)
{
string year = DateTime.Now.Year.ToString();
string month = DateTime.Now.Month.ToString("00");
string day = DateTime.Now.Day.ToString("00");

string fileFolder = string.Concat("Upload/", year, "/", month, "/", day);
string path = HostingEnvironment.MapPath("~/" + fileFolder);

if (!Directory.Exists(path))
{
//在Upload目录下创建了一个文件夹
Directory.CreateDirectory(path);
}
string imagename = image.FileName;
string fileType = Path.GetExtension(image.FileName).ToLower();
if (fileType == ".jpeg" || fileType == ".jpg" || fileType == ".png" || fileType == ".gif")
{
Random ran = new Random();
string ename = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ran.Next(9999);
//上传后生成的图片名
string name = ename + fileType;

image.SaveAs(Path.Combine(path, name));
string imageurl = string.Concat(HostingEnvironment.ApplicationVirtualPath, fileFolder, "/", name);

//生成标题图片
//缩略图名
string thumbName = ename + "_500x200" + fileType;

//缩略图绝对路径
string thumbPath = string.Concat(HostingEnvironment.ApplicationPhysicalPath, fileFolder, "/", thumbName);

// 设置缩略图保存路径
FileStream stream = new FileStream(Path.GetFullPath(thumbPath), FileMode.OpenOrCreate);

// 缩放上传的文件
Image OrigImage = Image.FromStream(image.InputStream);



// 创建缩略图对象
Bitmap TempBitmap = new Bitmap(550, 200);
// 创建缩略图画质
Graphics NewImage = Graphics.FromImage(TempBitmap);
NewImage.CompositingQuality = CompositingQuality.HighQuality;
NewImage.SmoothingMode = SmoothingMode.HighQuality;
NewImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 创建Rectangle对象进行绘制
Rectangle imageRectangle = new Rectangle(0, 0, 550, 200);
NewImage.DrawImage(OrigImage, imageRectangle);
// 保存缩略图
TempBitmap.Save(stream, OrigImage.RawFormat);
// 释放资源
NewImage.Dispose();
TempBitmap.Dispose();
OrigImage.Dispose();
stream.Close();
stream.Dispose();

newArticle.ImageUrl = imageurl;
}
}


但是这样,直接指定了宽、高,难免会造成图片变形。所以想改造在成宽度固定550,然后高度自适应。相当于windows中的图片查看器,改变窗体大小,图片也会相应的改变,图片不变形。
这个应该就是等比例缩话吧,上面的代码如何改造 ?
...全文
807 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
anzhiqiang_touzi 2012-03-28
  • 打赏
  • 举报
回复
postcha 2012-03-28
  • 打赏
  • 举报
回复
我怎么觉得大家的方法都复杂了呢?

// 创建缩略图对象
Bitmap TempBitmap = new Bitmap(550, 550 * OrigImage.Height / OrigImage.Width);

// 创建Rectangle对象进行绘制
Rectangle imageRectangle = new Rectangle(0, 0, 550, 550 * OrigImage.Height / OrigImage.Width);

我就修改了上面这样的二句,图片就不变形了。
ycproc 2012-03-28
  • 打赏
  • 举报
回复

/// <summary>
/// 根据源图片生成缩略图
/// </summary>
/// <param name="imgPath_old">源图(大图)物理路径</param>
/// <param name="imgPath_new">缩略图物理路径(生成的缩略图将保存到该物理位置)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">缩略图缩放模式(取值"HW":指定高宽缩放,可能变形;取值"W":按指定宽度,高度按比例缩放;取值"H":按指定高度,宽度按比例缩放;取值"Cut":按指定高度和宽度裁剪,不变形);取值"DB":等比缩放,以值较大的作为标准进行等比缩放</param>
/// <param name="type">即将生成缩略图的文件的扩展名(仅限:JPG、GIF、PNG、BMP)</param>
public static void MakeThumbnail(string imgPath_old, string imgPath_new, int width, int height, string mode, string imageType, int xx, int yy)
{
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; switch (mode)
{
case "HW": //指定高宽压缩
if ((double)img.Width / (double)img.Height > (double)width / (double)height)//判断图形是什么形状
{
towidth = width;
toheight = img.Height * width / img.Width;
}
else if ((double)img.Width / (double)img.Height == (double)width / (double)height)
{
towidth = width;
toheight = height;
}
else
{
toheight = height;
towidth = img.Width * height / img.Height;
}
break;
case "W": //指定宽,高按比例
toheight = img.Height * width / img.Width;
break;
case "H": //指定高,宽按比例
towidth = img.Width * height / img.Height;
break;
case "Cut": //指定高宽裁减(不变形)
if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
{
oh = img.Height;
ow = img.Height * towidth / toheight;
y = yy; x = (img.Width - ow) / 2;
}
else
{
ow = img.Width;
oh = img.Width * height / towidth;
x = xx; y = (img.Height - oh) / 2;
} break;
case "DB": // 按值较大的进行等比缩放(不变形)
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;
} 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(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存缩略图
switch (imageType.ToLower())
{
case "gif":
img.Save(imgPath_new, ImageFormat.Jpeg);//生成缩略图
break;
case "jpg":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "bmp":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "png":
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Png);
break;
default:
bitmap.Save(imgPath_new, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
////保存缩略图
// bitmap.Save(imgPath_new);
}
catch (System.Exception e)
{
throw e;
}
finally
{
img.Dispose();
bitmap.Dispose(); g.Dispose();
}
}
postcha 2012-03-28
  • 打赏
  • 举报
回复
楼上的也太贵了。。
huayy 2012-03-27
  • 打赏
  • 举报
回复
等比缩放,在展示的时候,有那种DIV+CSS可以按比例展示的。
类似 http://www.ktlm.cn/student.html
EnForGrass 2012-03-27
  • 打赏
  • 举报
回复
Asp.net真正生成高质量不变形缩略图片
http://www.cnblogs.com/leeolevis/archive/2009/02/03/1383132.html
adonis_net 2012-03-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 porschev 的回复:]
C# code

/// <summary>
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxWidth">需要缩小到的宽度</param>
/// <param name="maxHeight">需要缩小到的高度</param>
/// <param name="imageOriginalWidth">图片的原始宽度</para……
[/Quote]
同意.
porschev 2012-03-27
  • 打赏
  • 举报
回复


/// <summary>
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxWidth">需要缩小到的宽度</param>
/// <param name="maxHeight">需要缩小到的高度</param>
/// <param name="imageOriginalWidth">图片的原始宽度</param>
/// <param name="imageOriginalHeight">图片的原始高度</param>
/// <returns>返回图片按等比例缩小后的实际大小</returns>
public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(imageOriginalWidth);
double sh = Convert.ToDouble(imageOriginalHeight);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
/// <summary>
/// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。
/// </summary>
/// <param name="originalImage">原始图片</param>
/// <param name="thumMaxWidth">缩略图的宽度</param>
/// <param name="thumMaxHeight">缩略图的高度</param>
/// <returns>返回缩略图的Image对象</returns>
public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight)
{
Size thumRealSize = Size.Empty;
System.Drawing.Image newImage = originalImage;
Graphics graphics = null;
try
{
thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);
newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);
graphics = Graphics.FromImage(newImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.Clear(Color.Transparent);
graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);
}
catch { }
finally
{
if (graphics != null)
{
graphics.Dispose();
graphics = null;
}
}
return newImage;
}

nitaiyoucala 2012-03-27
  • 打赏
  • 举报
回复
要想不变形,只能截取了。
tptptp00 2012-03-27
  • 打赏
  • 举报
回复
想完全不变形是不可能的
postcha 2012-03-27
  • 打赏
  • 举报
回复
1楼的代码不错,马上试一下。
凡诺企业网站管理系统PHP版是以php+MySQL进行开发的php企业网站管理系统。 凡诺企业网站管理系统功能简介: 一、无限级频道设置,可自由指定导航频道。 二、多种频道类型自由组合。 三、大气的宽屏扁平化模版。 四、后台操作极其简单。 平台需求 IIS/Apache/Nginx + PHP5.2 PHP5.3 PHP5.4 + MySQL5 后台路径:/admin 用户密码:admin  admin 凡诺企业网站管理系统 更新日志: 3.5更新记录 一、编辑器中增加可上传mp4格式 二、增大上传文件大小限制到1024M 3.4更新记录 一、修正后台参数存在的安全隐患。 3.3更新记录 一、修正后台参数存在的安全隐患。 3.2更新记录 一、后台加入安全过滤规则,防止泄露后台后被入侵,然而也可能会对部分正常内容误报。 二、详情中多图采用编辑器模式,现在可以拖动排序了。 3.1更新记录 一、修正管理员不能删除的bug 3.0更新记录 一、后台新增缩略图比例设定,满足更多人的定制需求。 二、新增数据库备份功能。 三、前台组图增加图片链接和alt title属性 四、频道增加独立的缩率图宽高比设置,优先级高于全局设置,可以单独指定某个频道的缩略图比例在不变形的情况下缩放。 五、当基本设置中的QQ 电话 二维码 留空时,前台会自动隐藏相关图标。 六、频道模型和详情模型保持和收费版完全一致,方便升级 七、其他一些安全细节更新,如组件升级等

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧