C#缩略图 Asp.net生成缩略图

/// <summary>
    ///  创建缩略图
    /// </summary>
    /// <param name="oldFilePath">旧文件地址</param>
    /// <param name="newFilePath">新文件地址</param>
    public void CreatedImg(string oldFilePath, string newFilePath)
    {
        System.Drawing.Image originImage = System.Drawing.Image.FromFile(oldFilePath);
        int toWidth = 210;
        int toHeight = 0;
        int x = 0;
        int y = 0;
        int ow = originImage.Width;
        int oh = originImage.Height;
        switch ("W")
        {
            case "HW":

                break;

            case "W":
                toHeight = oh * toWidth / ow;
                break;

            case "H":
                toWidth = ow * toHeight / oh;
                break;

            case "Cut":
                break;

            default:
                break;
        }
        System.Drawing.Image bitmap = new Bitmap(toWidth, toHeight);
        Graphics g = Graphics.FromImage(bitmap);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(originImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
        string path = Server.MapPath("https://files.cnblogs.com/ThumbnailsImage");
        try
        {
            bitmap.Save(path + "/" + newFilePath);
        }
        catch
        {
            bitmap.Save(path + "/" + newFilePath,ImageFormat.Jpeg);
        }
        finally
        {
            originImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }

    }
posted @ 2012-08-02 23:14  cheng1234xiang  阅读(202)  评论(0编辑  收藏  举报