生成缩略图多种方法。

时间:2016/12/9 15:16:00来源:互联网 作者:flyso 点击: 2269 次

 public static void MakeThumbnail(string ImagePath, int w, int h, string SavePath = "", ThumbnailMode mode = ThumbnailMode.HW)

    {

        Image originalImage = Image.FromFile(files.MapPath(ImagePath));

        int towidth = w;

        int toheight = h;

        int x = 0;

        int y = 0;

        int ow = originalImage.Width;

        int oh = originalImage.Height;

        switch (mode)

        {

            case ThumbnailMode.HW://指定高宽缩放(可能变形)                 

                break;

            case ThumbnailMode.W://指定宽,高按比例                     

                toheight = originalImage.Height * w / originalImage.Width;

                break;

            case ThumbnailMode.H://指定高,宽按比例 

                towidth = originalImage.Width * h / originalImage.Height;

                break;

            case ThumbnailMode.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 * h / towidth;

                    x = 0;

                    y = (originalImage.Height - oh) / 2;

                }

                break;

            default:

                break;

        }

        //新建一个bmp图片 

        Image bitmap = new Bitmap(towidth, toheight);

        //新建一个画板 

        Graphics g = Graphics.FromImage(bitmap);

        //设置高质量插值法 

        g.InterpolationMode = InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度 

        g.SmoothingMode = SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充 

        g.Clear(Color.Transparent);

        //在指定位置并且按指定大小绘制原图片的指定部分 

        g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),

            new Rectangle(x, y, ow, oh),

            GraphicsUnit.Pixel);

        try

        {

            string imgtype = "";

            if (SavePath == "v")

            {

                using (MemoryStream ms = new MemoryStream())

                {

                    bitmap.Save(ms, Fun.getImageFormat(ImagePath, ref imgtype));

                    HttpResponse response = HttpContext.Current.Response;

                    response.ClearContent();

                    response.ContentType = imgtype;

                    response.BinaryWrite(ms.ToArray());

                }

            }

            else

            {

                if (string.IsNullOrEmpty(SavePath))

                    SavePath = ImagePath.Replace(".", "_small.");

                originalImage.Dispose();

                bitmap.Save(files.MapPath(SavePath), Fun.getImageFormat(ImagePath, ref imgtype));

            }

        }

        catch (System.Exception e)

        {

            throw e;

        }

        finally

        {

            originalImage.Dispose();

            bitmap.Dispose();

            g.Dispose();

        }

    }


    /// <summary>

    /// 生成缩略图

    /// </summary>

    /// <param name="originalImagePath">源图路径(物理路径)</param>

    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>

    /// <param name="width">缩略图宽度</param>

    /// <param name="height">缩略图高度</param> 


    public void CreateMinImage(string originalImagePath, string thumbnailPath, int width, int height)

    {

        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));


        int towidth = width;

        int toheight = height;


        int ow = originalImage.Width;

        int oh = originalImage.Height;


        //新建一个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));


        try

        {

            //以jpg格式保存缩略图

            string FileExt = Path.GetFileNameWithoutExtension(originalImagePath);

            //这里报错               bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        }

        catch (System.Exception e)

        {

            throw e;

        }

        finally

        {

            originalImage.Dispose();

            bitmap.Dispose();

            g.Dispose();

        }


    }


    //修改后的代码

    public void CreateMinImageAndDel(string originalImagePath, string thumbnailPath, int width, int height)

    {

        Graphics draw = null;

        string FileExt = "";


        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(originalImagePath));


        int towidth = width;

        int toheight = height;


        int ow = originalImage.Width;

        int oh = originalImage.Height;


        //新建一个bmp图片

        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

        System.Drawing.Image bitmap2 = 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));


        try

        {

            //以jpg格式保存缩略图

            FileExt = Path.GetFileNameWithoutExtension(originalImagePath);

            //用新建立的image对象拷贝bitmap对象 让g对象可以释放资源

            draw = Graphics.FromImage(bitmap2);

            draw.DrawImage(bitmap, 0, 0);


        }

        catch (System.Exception e)

        {

            throw e;

        }

        finally

        {

            originalImage.Dispose();

            bitmap.Dispose();

            g.Dispose();

            //保存调整在这里即可

            bitmap2.Save(HttpContext.Current.Server.MapPath(thumbnailPath) + FileExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        }

    }


Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3