Asp.Net中图片大小的缩放
2015-11-13 10:59
621 查看
在Asp.Net中显示图片的时候,如果给定一个固定大小的容器,如<Table>,图片的大小如何根据容器的大小进行比例缩放呢。以下是一个比较简单的函数,根据图片的宽高比例进行计算,缩放后保持比例不变。
//ViewSize 外框大小
//ImageSize 图片的实际大小
public Size Resize(Size ViewSize, Size ImageSize)
{
Size MySize = new Size();
if(ViewSize.Width > ViewSize.Height)//宽大于高
{
if(ImageSize.Width > ImageSize.Height)//按比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
if(ViewSize.Height / (float)ViewSize.Width < scale)
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height / scale);
}
else
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
}
else if(ImageSize.Height > ImageSize.Width)//非比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
else if(ViewSize.Width < ViewSize.Height)//高大于宽
{
if(ImageSize.Width < ImageSize.Height)//按比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
if(ViewSize.Width / (float)ViewSize.Height < scale)
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width / scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
}
else if(ImageSize.Height < ImageSize.Width)//非比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else
{
MySize.Height = ViewSize.Width;
MySize.Width = ViewSize.Width;
}
}
else
{
if(ImageSize.Width > ImageSize.Height)//宽大于高
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else if(ImageSize.Width < ImageSize.Height)//高大于宽
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
return MySize;
}
//ViewSize 外框大小
//ImageSize 图片的实际大小
public Size Resize(Size ViewSize, Size ImageSize)
{
Size MySize = new Size();
if(ViewSize.Width > ViewSize.Height)//宽大于高
{
if(ImageSize.Width > ImageSize.Height)//按比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
if(ViewSize.Height / (float)ViewSize.Width < scale)
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height / scale);
}
else
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
}
else if(ImageSize.Height > ImageSize.Width)//非比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
else if(ViewSize.Width < ViewSize.Height)//高大于宽
{
if(ImageSize.Width < ImageSize.Height)//按比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
if(ViewSize.Width / (float)ViewSize.Height < scale)
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width / scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
}
else if(ImageSize.Height < ImageSize.Width)//非比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else
{
MySize.Height = ViewSize.Width;
MySize.Width = ViewSize.Width;
}
}
else
{
if(ImageSize.Width > ImageSize.Height)//宽大于高
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else if(ImageSize.Width < ImageSize.Height)//高大于宽
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
return MySize;
}
相关文章推荐
- ASP.NET WebService的图片验证码服务
- ASP.NET调用JAVA的CXF生成的Webservice,并带有Soapheader验证的解决方案
- ASP.NET在IIS7.5(IIS7)配置伪静态
- ExtAspNet2.1.0版本
- IBatis.net在asp.net MVC下的使用
- ASP.NET 抓取网页
- 摘抄——OWASP_Code_Review_Guide-V1_1 (1)
- asp.net 子页面关闭刷新父页面
- asp.net 子页面关闭刷新父页面
- asp.net夜话之五:Page类和回调技术
- (easyui datagrid+mvc+json)之asp.net分页查询
- Asp.net页面间传值方式汇总
- ASP.NET页面通过URL传递参数(一)(转载)
- Request 分别获取具有相同 name 属性表单元素值—— 怀念 Classic ASP (转自博客园)
- ASP.NET 5 开发者的五个阶段
- Struts2整合jasperreport缓存处理方法
- asp.net下载文件几种方式
- Asp.net获取程序路径
- IIS对ASP的解析问题
- ASP.NET 页生命周期概述