网站建设知识

我们将想法与焦点与您一起共享

当前位置:深圳网站建设 > 网站建设知识> 独占网络生成图片小图的代码

独占网络生成图片小图的代码

2015-2-7 10:25:42 独占网络 网站建设知识
       我们在后台上传图片的时候经常会生成几张小图,因为我们网站上面都会有几个小图,我们不可以让用户一张图片要多次处理然后上传一张图片吧,这个不合理,所以我们采用生成小图的方法来完成这一个效果,下面就是我们实现这个方法的代码。
/// <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)
{
    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(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图片
    System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    //新建一个画板
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    //设置高质量插值法
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //设置高质量,低速度呈现平滑程度
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
    g.Clear(System.Drawing.Color.Transparent);
    //在指定位置并且按指定大小绘制原图片的指定部分
    g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
        new System.Drawing.Rectangle(x, y, ow, oh),
        System.Drawing.GraphicsUnit.Pixel);
    try
    {
        originalImage.Dispose();
        string toFileFullPath = HttpContext.Current.Server.MapPath(thumbnailPath.Substring(0, thumbnailPath.LastIndexOf("/")));
        //检查是否有该路径没有就创建
        if (!Directory.Exists(toFileFullPath))
        {
            Directory.CreateDirectory(toFileFullPath);
        }
        //保存缩略图
        bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath));
    }
    catch (System.Exception e)
    {
        //异常
        throw e;
    }
    finally
    {
        //关闭信息
        bitmap.Dispose();
        g.Dispose();
    }
}
如有不懂,联系独占网络(http://www.sz886.com)
始终专注高端网站建设服务 网站建设案例服务方案联系

联系我们

135-3000-5572
151-1257-9390

地址:深圳市龙岗区横岗街道联旺大厦5楼

邮箱:chenmh@sz886.com

我们的优势

12年建站服务经验自主开发后台CMS开发项目不外包无隐形收费

服务198家上市企业服务行业龙头超过70家 营销型网站建设完备的项目流程管理体系网页设计与网站开发技术并重

关于独占网络

十多年来,凭借对设计的深刻理解,对互联网营销趋势的敏锐洞察,独占网络不断修正服务导向,完善创作品格,始终注重专业探索,为所有合作企业机构全力以赴,确保网站的视觉体验与营销转化能力。

我们是一群对互联网抱有执着信念的技术控。从2012年到现在,公司从未建过强大的营销团队,却凭着作品与服务不断保持增长,做到了"桃李不言,下自成蹊。" 我们坚信:每个客户都是一条渠道,每个案例都是一个广告,为企业插上翅膀,助力企业转型升级,我们已做好准备。

专业团队为您提供深圳网站建设、深圳网站制作、深圳营销型网站建设、外贸网站建设等服务,深圳建网站就找独占网络 | 12年专注网站建设

深圳市独占网络科技有限公司 |备案号:粤ICP备15007128| Copyright 2023,ALL Rights Reserved www.sz886.com | Copyright 2023版权所有
Hi,Are you ready? 如果您有意向
请于我们取得联系。

有一个互联网项目想和我们谈谈吗?
您可以填写右边的表格,让我们了解您的项目需求,这是一个良好的开始,我们将会尽快与你取得联系。当然也欢迎您给我们写信或是打电话,让我们听到你的声音!

独占-整合互联网营销

地址:深圳市龙岗区横岗地铁D出口联旺大厦5楼
大客户专线:深圳 13530005572
售前QQ:1447567909
E-mail: sales@sz886.cn

合作意向表
您希望我们为您提供什么服务?