zoukankan      html  css  js  c++  java
  • asp.net 为指定的图片生成缩图

    程序代码 程序代码
    #region 为指定的图片生成缩略图
            /// <summary>
            /// 为指定的图片生成缩图
            /// <para>color != null,则不变形</para>
            /// </summary>
            /// <param name="sourcepath">源图的完整硬盘路径,包括文件名</param>
            /// <param name="maxwidth">缩图宽度</param>
            /// <param name="maxheight">缩图高度</param>
            /// <param name="color">指定背景色,可传null</param>
            /// <param name="bmpath">缩图的完整硬盘目录,以/结束,不包括文件名</param>
            /// <returns>是否成功</returns>
            public static bool Thumbnail(string sourcepath, int maxwidth, int maxheight, Color color, string bmpath)
            {
                string filename = Path.GetFileNameWithoutExtension(sourcepath); //拿到没有后缀的文件名
                string fileExt = Path.GetExtension(sourcepath).ToLower(); //取得扩展名
                if (fileExt.IsNullOrEmpty()) //如果取不到文件后缀,就认为传入路径是错误的
                    return false;

                int ActualWidth = 0;  //图片实际宽度
                int ActualHeight = 0; //图片实际高度
                int Srcx = 0;         //图片左边距
                int Srcy = 0;         //图片上边距

                Image newImg;            
                using (Image oriImg = Image.FromFile(sourcepath))
                {
                    if ((maxwidth < oriImg.Width) || (maxheight < oriImg.Height)) //执行按比例缩小
                    {
                        if (maxwidth.toString() / maxheight.toString() > oriImg.Width.toString() / oriImg.Height.toString(0f)) //以高度为准计算
                        {
                            ActualWidth = maxheight * oriImg.Width / oriImg.Height;
                            ActualHeight = maxheight;
                            Srcx = (maxwidth - ActualWidth) / 2;
                        }
                        else
                        {
                            ActualWidth = maxwidth;
                            ActualHeight = maxwidth * oriImg.Height / oriImg.Width;
                            Srcy = (maxheight - ActualHeight) / 2;
                        }
                    }
                    else
                    {
                        ActualWidth = oriImg.Width;
                        ActualHeight = oriImg.Height;
                        Srcx = (maxwidth - ActualWidth) / 2;
                        Srcy = (maxheight - ActualHeight) / 2;
                    } //以上操作完成后,得到缩图的宽与高,下面生成缩图,并保存到缩图文件夹                

                    //根据颜色判断是否产生固定大小的缩图
                    newImg = color == null ? new Bitmap(ActualWidth, ActualHeight) : new Bitmap(maxwidth, maxheight);
                    using (Graphics g = Graphics.FromImage(newImg)) //把新图形加载进Graphice对象中
                    {
                        if (color != null)
                            g.Clear(color); //声明背景色
                        else
                        {
                            Srcx = 0;
                            Srcy = 0;
                        }

                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic; //指定高质量插值法,关键在于HighQualityBicubic
                        g.DrawImage(oriImg, new Rectangle(Srcx, Srcy, ActualWidth, ActualHeight), 0, 0, oriImg.Width, oriImg.Height, GraphicsUnit.Pixel);
                    }
                } //到这里,源图,画板均可以释放了

                switch (fileExt)
                {
                    case ".jpg":
                        newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
                        break;
                    case ".gif":
                        newImg.Save(bmpath + filename + ".gif", ImageFormat.Gif);
                        break;
                    case ".png":
                        newImg.Save(bmpath + filename + ".png", ImageFormat.Png);
                        break;
                    default:
                        newImg.Save(bmpath + filename + ".jpg", ImageFormat.Jpeg);
                        break;
                }
                newImg.Dispose();
                return true;
            }
            #endregion
  • 相关阅读:
    java中的上转型对象
    java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
    接口作为参数,不同的接口调用不同的方法,例如:输出“I love Game”或输出“我喜欢游戏”
    JavaScript--原型链
    JavaScript--clientX,clientY、pageX,pageY、offsetLeft,offsetTop/offsetWidth,offsetHeight、scrollLeft,scrollTop/scrollWidth,scrollHeight、clientHeight,clientWidth区别
    JavaScript--结合CSS变形、缩放能拖拽的登录框
    JavaScript--放大镜
    JavaScript--返回顶部方法:锚链接、行内式js写法、外链式、内嵌式
    JavaScript--封装好的运动函数+旋转木马例子
    JavaScript--漏写var却还能使用标签
  • 原文地址:https://www.cnblogs.com/sontin/p/1929787.html
Copyright © 2011-2022 走看看