生成缩略图

 方法一:

 /// <summary>
    /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径
    /// </summary>
    /// <param name="Width">缩略图的宽度</param>
    /// <param name="Height">缩略图的高度</param>
    /// <param name="Height">原始图路径 参数格式:D:/Images/filename.jpg</param>
    /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:/Images/filename.jpg</param>
    ///  <param name="ext">后缀名</param>
    /// <returns>成功返回true,否则返回false</returns>
    public bool GetReducedImage(int Width, int Height, string ImageFileName, string targetFilePath,string ext)
    {

        System.Drawing.Image ReducedImage = System.Drawing.Image.FromFile(ImageFileName);
        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(Width, Height);

        //新建一个画板
        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(ReducedImage, new Rectangle(0, 0, Width, Height),
            new Rectangle(0, 0, ReducedImage.Width, ReducedImage.Height),
            GraphicsUnit.Pixel);

        try
        {
            以jpg格式保存缩略图
            //bitmap.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //以原始图片格式保存缩略图
            switch (ext)
            {
                case ".jpg": bitmap.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                case ".gif": bitmap.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Gif); break;
                case ".bmp": bitmap.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Bmp); break;
                case ".png": bitmap.Save(targetFilePath, System.Drawing.Imaging.ImageFormat.Png); break;
            }
            return true;
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            ReducedImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

 

方法二:


    /// <summary>
    /// 按比例生成缩略图,将缩略图文件保存到指定的路径
    /// </summary>
    /// <param name="maxwidth">缩略图最大宽度</param>
    /// <param name="maxheight">缩略图的最大高度</param>
    /// <param name="ImageFileName">原始图路径</param>
    /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:/Images/filename.jpg</param>
    ///  <param name="ext">后缀名</param>
    /// <param name="width">原图的宽度</param>
    /// <param name="height">原图的高度</param>
    /// <param name="fileUpload1">上传文件控件</param>

    public  void GetReducedImage(decimal maxwidth, decimal maxheight, string ImageFileName, string targetFilePath, string ext, decimal width, decimal height, FileUpload fileUpload1)
    {
        decimal oldwidth = 0;
        decimal oldheight = 0;
        bool flag = true;

        if (width > height)
        {
            if (width > maxwidth)
            {
                oldwidth = width;
                height = height * (maxwidth / oldwidth);
                width = maxwidth;

            }
            else
            {
                flag = false;
            }
        }
        else
        {
            if (height > maxheight)
            {
                oldheight = height;
                width = width * (maxheight / oldheight);
                height = maxheight;
            }
            else
            {
                flag = false;
            }
        }
        if (flag)
        {
            GetReducedImage((int)width, (int)height, ImageFileName, targetFilePath, ext);
        }
        else
        {
            fileUpload1.PostedFile.SaveAs(targetFilePath);
        }

    }

 

示例:

  this.FileUpload2.PostedFile.SaveAs(Server.MapPath("~/yuanImage/" + n + ext));


                //获取原图的高和宽
                System.Drawing.Image yimage = System.Drawing.Image.FromFile(Server.MapPath("~/yuanImage/" + n + ext));
                decimal height = yimage.Height;
                decimal width = yimage.Width;

              
                decimal maxwidth = 650;
                decimal maxheight = 580;

                fileName = "suoluetu/" + n + ext;
                file = "yuanImage/" + n + ext;

                GetReducedImage(maxwidth, maxheight,Server.MapPath("~/"+ file),Server.MapPath("~/"+ fileName) , ext, width, height, this.FileUpload2);

           
                //释放资源才能删除图片
                yimage.Dispose();
                System.IO.File.Delete(Server.MapPath("~/" + file));

 

 

方法三:

  protected void Button1_Click(object sender, EventArgs e)
    {
        //上传原始图
        string fileName= this.FileUpload1.FileName;
        //后缀
        string ext=System.IO.Path.GetExtension(fileName);
        fileName=DateTime.Now.ToString("yyyyMMddhhmmss")+ext;
        //上传原始图片的路径
        string ypath=Server.MapPath("~/images/big/"+fileName);
        //上传缩略图的路径
        string spath=Server.MapPath("~/images/small/"+fileName);
        this.FileUpload1.PostedFile.SaveAs(ypath);


       
        //创建原图
        System.Drawing.Image yImage = System.Drawing.Image.FromFile(ypath);
        //获取原图的高宽
        decimal height= yImage.Height;
        decimal width = yImage.Width;
       

        //设置缩略图的宽度比例
        decimal bili = Math.Round(200 / width, 2);//把小数200 / width保留两位小数
        //根据比例计算缩略图的高
       height = height *bili;
       //int h = (int)height;
        System.Drawing.Image.GetThumbnailImageAbort abort = null ;

        //返回原图片的缩略图
        System.Drawing.Image sImage= yImage.GetThumbnailImage(200, (int)height, abort,new System.IntPtr());


        sImage.Save(spath);
        yImage.Dispose();
        sImage.Dispose();

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值