实现修改图片大小 相关帮助类

帮助类

http://www.cnblogs.com/Kiss920Zz/archive/2011/12/26/2302223.html



做了个在上传图片的时候,自动修改图片大小的功能,其实一开始是想做类似京东商城的放大镜,然后发现做那样的需要相同的图片大小不同的的三张,于是想到是不是可以再上传图片的时候只上传一张,自动生成三张大小的图片,最后就做了这个功能!

#region 图片上传

    /// <summary>
    /// 图片上传0
    /// </summary>
    /// <returns></returns>
    private string Upload_Images0()
    {
        Boolean fileOK = false;
        string imageFile = "";
        String path = Server.MapPath("~/adpic/");  //设置服务器上传的路径,即文件上传的位置
        if (fulImage.PostedFile.ContentLength / 1024 < 2000)
        {
            if (fulImage.HasFile)
            {
                //获取上传文件类型
                String fileExtension = System.IO.Path.GetExtension(fulImage.FileName).ToLower();
                //判断允许上传图片类型
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                        break;
                    }
                }

                if (fileOK)
                {
                    try
                    {
                        //把当前时间取出,组成字符串,加入文件名称,防止重复命名
                        string fileName = DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "").Replace("/", "") + fulImage.FileName;
                        fulImage.PostedFile.SaveAs(path + fileName);
                        imageFile = "adpic/" + fileName;

                        //自动生成小图
                        MakeThumNail(path + fileName, Server.MapPath("~/imagess/") + fileName, 600, 500, "W");
                        
                        //自动生成小图
                        MakeThumNail(path + fileName, Server.MapPath("~/image/") + fileName, 100, 50, "W");
                        //自动生成中图
                        MakeThumNail(path + fileName, Server.MapPath("~/images/") + fileName, 200, 50, "W");

                        //删除原图
                        System.IO.File.Delete(path + fileName);
                    }
                    catch (Exception e)
                    {
                        Response.Write("<script>alert('对不起,图片不能上传!')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('对不起,不能接受这种文件类型!')</script>");
                }
            }

        }
        else
        {
            Response.Write("<script>alert('对不起,图片过大不能上传!')</script>");
        }
        return imageFile;
    }

    #endregion 

    # region 图片处理

    /// <summary>
    /// 缩放图像
    /// </summary>
    /// <param name="originalImagePath">图片原始路径</param>
    /// <param name="thumNailPath">保存路径</param>
    /// <param name="width">缩放图的宽</param>
    /// <param name="height">缩放图的高</param>
    /// <param name="model">缩放模式</param>
    public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

        int thumWidth = width;      //缩略图的宽度
        int thumHeight = height;    //缩略图的高度

        int x = 0;
        int y = 0;

        int originalWidth = originalImage.Width;    //原始图片的宽度
        int originalHeight = originalImage.Height;  //原始图片的高度

        switch (model)
        {
            case "HW":      //指定高宽缩放,可能变形
                break;
            case "W":       //指定宽度,高度按照比例缩放
                thumHeight = originalImage.Height * width / originalImage.Width;
                break;
            case "H":       //指定高度,宽度按照等比例缩放
                thumWidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut":
                if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
                {
                    originalHeight = originalImage.Height;
                    originalWidth = originalImage.Height * thumWidth / thumHeight;
                    y = 0;
                    x = (originalImage.Width - originalWidth) / 2;
                }
                else
                {
                    originalWidth = originalImage.Width;
                    originalHeight = originalWidth * height / thumWidth;
                    x = 0;
                    y = (originalImage.Height - originalHeight) / 2;
                }
                break;
            default:
                break;
        }

        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight);

        //新建一个画板
        System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);

        //设置高质量查值法
        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充
        graphic.Clear(System.Drawing.Color.Transparent);

        //在指定位置并且按指定大小绘制原图片的指定部分
        graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);

        try
        {
            bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            graphic.Dispose();
        }
    }
        
    # endregion
原文地址:https://www.cnblogs.com/qingrp-2015930/p/4852360.html