Asp.net 图片等比例生成缩略图,外带一个数学公式
今天在做一个网站后台,有个将上传图片生成缩略图的功能,生成方法是以前自己写的,结果发现不少图片没能正确生成,于是就调试其两年多前写的代码,虽然当年写了不少注释,不过还是花了我不少时间,因为期间我花了不少时间来证明一个数学公式了,这个公式应该是初中的知识!想想写了三,四的年的程序,基本上除了小学的算术,貌似就没用到什么数据知识了.
这是一个不等式
设 A=(y*a/x)-b , B=(x * b/y)-a ,其中y,x,a,b 多为大于零的实数,
证明在符合取值的情况下,A跟B中必定存在 A>=0 并且 B<=0 ,或者A<=0 并且 B>=0
证明相信不难,因为这的确是一个初中时的不等公式,下面是问题的来源
图片等比例生产缩略图的代码
//------------------------------
public static bool ThumImage(string srcPath, string destPath, double width, double height)
{
System.Drawing.Image img = new Bitmap(srcPath);
//生成图片大小必需小于原图
if (img.Width < width)
{
width = img.Width;
}
if (img.Height < height)
{
height = img.Height;
}
//删除的高度,与宽度
double cutWidth, cutHeight;
cutWidth = (img.Width * height / img.Height - width); //宽度切割,高度缩放
cutHeight = (img.Height * width / img.Width - height);//高度切割,宽度缩放
byte flag = 0;//0 截高,1 截宽
//这里的截宽是指缩略图的宽将被截,不是指原图,
//1. 按原图比例,选择缩略图的高固定,计算出来的宽如果大于指定的宽,那么就按高固定,计算出来的宽来生成缩略图,再按给定大小截取
//2. 按原图比例,选择缩略图的宽固定,计算出来的高如果大于指定的高,那么就按宽固定,计算出来的高来生成缩略图,再按给定大小截取
//3. 因为长宽比只能取{1,>1,<1}三种情况
flag = (byte)(cutHeight <= cutWidth ? 0 : 1);
//System.Drawing.Image.GetThumbnailImageAbort myCallback=new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumImg;
if (flag == 0)
thumImg = new Bitmap(img, (int)width, (int)height + (int)cutHeight); //img.GetThumbnailImage((int)width, (int)height + (int)cutHeight, myCallback, IntPtr.Zero);
else
thumImg = new Bitmap(img, (int)width + (int)cutWidth, (int)height);// img.GetThumbnailImage((int)width + (int)cutWidth, (int)height, myCallback, IntPtr.Zero);
System.Drawing.Bitmap destImg = new Bitmap((int)width, (int)height);
Graphics g = Graphics.FromImage(destImg);
Rectangle rect = new Rectangle(0, 0, (int)width, (int)height);
g.DrawImage(thumImg, rect, rect, GraphicsUnit.Pixel);
g.Save();
destImg.Save(destPath, GetFormat(destPath));
thumImg.Dispose();
img.Dispose();
destImg.Dispose();
return true;
}
//----------------------------
这个方法的思路是,原图跟需要生成缩略图的长宽比例不一定相同,那么在长宽比例不同的情况下,直接生成会使图片变形,
为了防止这个情况发生,就需要根据原图比例来生成,我们先根据原图比例,生成一个略微比需要缩略图大的图片,接着将这个图片按指定的大小进行截取. 为实现这个需要,先根据指定缩略图的高计算出符合原图比例的宽,将这个宽跟需要生成缩略图的宽进行比较,如果计算出来的宽大于指定缩略图的宽,那么我们就按照 ,计算所得的宽跟缩略图的高创建一个位图,再根据需要的缩略图大小截取.如果计算出来的宽小于指定缩略图的宽,那么就使用指定缩略图的宽来计算符合比例的高,此时高必定大于需要生成缩略图的高,此时按计算所得的高跟需要生成缩略图的宽,进行上面的操作,因为高跟宽必定有一个符合上面的要求. 代码如下
if (flag == 0)
thumImg = new Bitmap(img, (int)width, (int)height + (int)cutHeight); //img.GetThumbnailImage((int)width, (int)height + (int)cutHeight, myCallback, IntPtr.Zero);
else
thumImg = new Bitmap(img, (int)width + (int)cutWidth, (int)height);// img.GetThumbnailImage((int)width + (int)cutWidth, (int)height, myCallback, IntPtr.Zero);
System.Drawing.Bitmap destImg = new Bitmap((int)width, (int)height);
Graphics g = Graphics.FromImage(destImg);
Rectangle rect = new Rectangle(0, 0, (int)width, (int)height);
g.DrawImage(thumImg, rect, rect, GraphicsUnit.Pixel);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述