ImageResizer上传图片,并批量切割不同大小的图片

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Joyhen/article/details/81588695

具体看官方demo,下面是测试代码

using ImageResizer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// getimg 的摘要说明
    /// </summary>
    public class getimg : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var ctxfiles = HttpContext.Current.Request.Files;
            if (ctxfiles == null) return;

            //Define the versions to generate
            Dictionary<string, string> versions = new Dictionary<string, string>();
            versions.Add("_thumb", "width=100&height=100&crop=auto&format=jpg"); //Crop to square thumbnail
            versions.Add("_medium", "maxwidth=400&maxheight=400&format=jpg"); //Fit inside 400x400 area, jpeg
            versions.Add("_large", "maxwidth=1900&maxheight=1900&format=jpg"); //Fit inside 1900x1200 area

            foreach (string fileKey in ctxfiles.Keys)
            {
                HttpPostedFile file = ctxfiles[fileKey];
                if (file.ContentLength <= 0)
                    continue;

                string uploadFolder = context.Server.MapPath("~/uploads");
                if (!Directory.Exists(uploadFolder))
                    Directory.CreateDirectory(uploadFolder);

                //Generate each version
                foreach (string suffix in versions.Keys)
                {
                    string fileName = Path.Combine(uploadFolder, Guid.NewGuid().ToString() + suffix);
                    ImageJob i = new ImageJob(file,
                        "~/uploads/<guid>.<ext>",
                        new Instructions(versions[suffix]),
                        false,
                        true
                    );
                    fileName = ImageBuilder.Current.Build(i).FinalPath;
                    //...
                }
            }

            context.Response.Write("img upload success!");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

展开阅读全文

没有更多推荐了,返回首页