首页 > 编程 > .NET > 正文

asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式

2024-07-10 13:04:39
字体:
来源:转载
供稿:网友

在网站开发时,生成缩略图是一个非常常见和实用的功能.以前在asp里只能借助com组件实现,现在在.net里可以利用框架的强大的类库轻松实现.下面帖出完整的代码(带详细注释),参考了网上的一些文章及.net sdk相关内容.qqroom网络家园的图片上传用到了所有的4种生成方式.

        /**//// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalimagepath">源图路径(物理路径)</param>
        /// <param name="thumbnailpath">缩略图路径(物理路径)</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>   
        public static void makethumbnail(string originalimagepath, string thumbnailpath, int width, int height, string mode)
        {
            image originalimage = image.fromfile(originalimagepath);
           
            int towidth = width;
            int toheight = height;
       
            int x = 0;
            int y = 0;
            int ow = originalimage.width;
            int oh = originalimage.height;       

            switch (mode)
            {       
                case "hw"://指定高宽缩放(可能变形)               
                    break;
                case "w"://指定宽,高按比例                   
                    toheight = originalimage.height * width/originalimage.width;
                    break;
                case "h"://指定高,宽按比例
                    towidth = originalimage.width * height/originalimage.height;                   
                    break;       
                case "cut"://指定高宽裁减(不变形)               
                    if((double)originalimage.width/(double)originalimage.height > (double)towidth/(double)toheight)
                    {
                        oh = originalimage.height;
                        ow = originalimage.height*towidth/toheight;
                        y = 0;
                        x = (originalimage.width - ow)/2;
                    }
                    else
                    {
                        ow = originalimage.width;
                        oh = originalimage.width*height/towidth;
                        x = 0;
                        y = (originalimage.height - oh)/2;
                    }
                    break;                   
                default :
                    break;
            }   
           
            //新建一个bmp图片
            image bitmap = new system.drawing.bitmap(towidth,toheight);

            //新建一个画板
            graphics g = system.drawing.graphics.fromimage(bitmap);

            //设置高质量插值法
            g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;

            //设置高质量,低速度呈现平滑程度
            g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;

            //清空画布并以透明背景色填充
            g.clear(color.transparent);       

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.drawimage(originalimage, new rectangle(0, 0, towidth, toheight),
                new rectangle(x, y, ow,oh),
                graphicsunit.pixel);

            try
            {           
                //以jpg格式保存缩略图
                bitmap.save(thumbnailpath, system.drawing.imaging.imageformat.jpeg);
            }
            catch(system.exception e)
            {
                throw e;
            }
            finally
            {
                originalimage.dispose();
                bitmap.dispose();                       
                g.dispose();
            }
        }

关键方法graphics.drawimage见ms-help://ms.netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm

 

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表