当前位置: 首页 > 工具软件 > ImageResizer > 使用案例 >

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

张炳
2023-12-01

具体看官方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;
            }
        }
    }
}

 

 类似资料: