首页 .NET/Web 生成缩略图片 ASP.NET

生成缩略图片 ASP.NET

作者:胡同里的砖头 围观群众:1549 更新于:2013-12-19

前台:

<asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />

后台:

using System.Drawing;
using System.IO;


/// <summary>
    /// 生成缩略图的方法
    /// </summary>
    /// <param name="Path">原始图片路径</param>
    /// <param name="StPath">生成图片存放的路径</param>
    /// <param name="width">生成图片的宽度</param>
    /// <param name="height">生成图片的高度</param>
    /// <param name="mode">生成的模式( "HW":指定高宽缩放;"W"://指定宽度,计算缩略图;"H":指定高度,计算缩略图;"CUT":)</param>
    /// <returns></returns>
    public static string MakeThumbImage(string Path, string StPath, int width, int height, string mode)
    {
        string messages = string.Empty;
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        int tw = width;
        int th = height;
        //原始图片的宽度和高度
        int sw = image.Width;
        int sh = image.Height;
        int x = 0, y = 0;
        switch (mode)
        {
            case "HW"://指定高宽缩放
                break;
            case "W"://按比例缩放,指定宽度,计算缩略图的高度
                th = image.Height * width / image.Width;
                break;
            case "H"://按比例缩放,指定高度,计算缩略图的高度
                tw = image.Width * height / image.Height;
                break;
            case "CUT":
                if ((double)tw / (double)th < (double)width / (double)height)
                {
                    sw = image.Width;
                    sh = image.Width * height / tw;
                    x = 0;
                    y = (image.Height - sh) / 2;
                }
                else
                {
                    sh = image.Height;
                    sw = image.Height * width / th;
                    y = 0;
                    x = (image.Width - sw) / 2;
                }
                break;
            default: break;
        }
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
        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(image,
            new System.Drawing.Rectangle(x, y, tw, th),
            new System.Drawing.Rectangle(x, y, sw, sh),
            System.Drawing.GraphicsUnit.Pixel);
        try
        {
            bitmap.Save(StPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex)
        {
            messages = ex.Message;
        }
        finally
        {
            image.Dispose();
            bitmap.Dispose();
            g.Dispose();
            if (messages == string.Empty)
            {
                messages = "成功生成!";
            }

        }

        return messages;
    }

  • 本文标题: 生成缩略图片 ASP.NET
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8