您的位置:首页 > 编程语言 > ASP

Asp.net 图片等比例生成缩略图,外带一个数学公式

2008-07-04 16:28 323 查看
   今天在做一个网站后台,有个将上传图片生成缩略图的功能,生成方法是以前自己写的,结果发现不少图片没能正确生成,于是就调试其两年多前写的代码,虽然当年写了不少注释,不过还是花了我不少时间,因为期间我花了不少时间来证明一个数学公式了,这个公式应该是初中的知识!想想写了三,四的年的程序,基本上除了小学的算术,貌似就没用到什么数据知识了.

 这是一个不等式 

 设 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: