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

111 篇文章 0 订阅

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java自带类进行批量修改图片大小的方法: 1. 使用ImageIO读取图片,获取图片的宽度和高度。 2. 创建一个BufferedImage对象,将原始图片绘制到该对象上。 3. 创建新的BufferedImage对象,设置新的宽度和高度。 4. 将原始图片绘制到新的BufferedImage对象上,并将其保存为新的图片文件。 ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageResizer { public static void resize(String inputImagePath, String outputImagePath, int scaledWidth, int scaledHeight) throws IOException { // Read the source image File inputFile = new File(inputImagePath); BufferedImage inputImage = ImageIO.read(inputFile); // Create the output image BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType()); // Scale the input image to the output image Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null); g2d.dispose(); // Save the scaled image as a JPEG File outputFile = new File(outputImagePath); ImageIO.write(outputImage, "jpg", outputFile); } } ``` 调用上述方法,可以批量修改图片大小: ```java public class BatchImageResizer { public static void main(String[] args) { String inputDir = "input"; String outputDir = "output"; int scaledWidth = 800; int scaledHeight = 600; File inputFolder = new File(inputDir); File[] inputFiles = inputFolder.listFiles(); for (File inputFile : inputFiles) { String inputFileName = inputFile.getName(); String outputFileName = inputFileName.substring(0, inputFileName.lastIndexOf(".")) + ".jpg"; String inputFilePath = inputDir + File.separator + inputFileName; String outputFilePath = outputDir + File.separator + outputFileName; try { ImageResizer.resize(inputFilePath, outputFilePath, scaledWidth, scaledHeight); } catch (IOException ex) { System.out.println("Error resizing the image: " + inputFileName); ex.printStackTrace(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值