如何在webapi中手动优化文件夹中的多个图像(减小文件大小)?

时间:2012-09-26 07:04:09

标签: .net image-processing .net-4.0 asp.net-mvc-4 asp.net-web-api

大家好我正在使用webapi我需要优化文件夹中的图片我已经Uploaded images into one folder才能上传到destination folder我需要在这里减少image size in KB总图像数量我正在优化single image in to 3 different sizes' (large, thumbnail,medium)' but image sizes are increased并保存在一个文件夹中 现在我需要优化那些图像,你可以帮助我 我怎么能这样做。实际上我是webapi的新手。 任何机构都可以有任何想法请帮助我,但我需要做的一件事this manually 提前谢谢你

这是我的控制器

public Task<HttpResponseMessage> Post()
        {
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var uploadFolder = HostingEnvironment.MapPath("~/App_Sprites/UploadFolder");
            uploadFolder = Path.Combine(uploadFolder, DateTime.Now.ToString("yyyyMMddhhmmssfff"));
            Directory.CreateDirectory(uploadFolder);
            var streamProvider = new PreserveFilenameMultipartFileStreamProvider(uploadFolder);

            return Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                foreach (var uploadedFile in Directory.GetFiles(uploadFolder))
                {
                    var thumbnail = Path.Combine(uploadFolder, "thumb-" + Path.GetFileName(uploadedFile));
                    var medium = Path.Combine(uploadFolder, "medium-" + Path.GetFileName(uploadedFile));
                    var large = Path.Combine(uploadFolder, "large-" + Path.GetFileName(uploadedFile));
                    ImageTools.Resize(uploadedFile, thumbnail, 80, 80);
                    ImageTools.Resize(uploadedFile, medium, 48, 48);
                    ImageTools.Resize(uploadedFile, large, 128, 128);
                }
                return Request.CreateResponse(HttpStatusCode.Accepted);
            });
        }
    }

abd这个我的班级文件

 public class ImageTools
    {
        public static void Resize(string original, string output, int width, int height)
        {
            using (var image = Image.FromFile(original))
            using (var thumbnail = new Bitmap(width, height))
            using (var graphics = Graphics.FromImage(thumbnail))
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;

                graphics.DrawImage(image, 0, 0, width, height);

                ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
                EncoderParameters encoderParameters;
                encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                thumbnail.Save(output, info[1], encoderParameters);
            }
        }
    }

类文件

 public class PreserveFilenameMultipartFileStreamProvider : MultipartFileStreamProvider
        {
            public PreserveFilenameMultipartFileStreamProvider(string rootPath)
                : base(rootPath)
            {
            }

            public override string GetLocalFileName(HttpContentHeaders headers)
            {
                return headers.ContentDisposition.FileName.Replace("\"", "");
            }
        }

这是我的布局页面

@using (Html.BeginRouteForm("DefaultApi", new { httproute = "", controller = "Upload" }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="image" multiple="multiple" accept="image/*" />
    <button type="submit">Upload</button>
}

我创建了一个文件夹现在图像保存在文件夹中但文件大小以kb增加但是我们需要减少plz帮助我

2 个答案:

答案 0 :(得分:3)

您应该为QualityLevel的调整大小方法添加一个参数,并使用此参数更改不同的渲染。

您正在调整图像的大小,将质量设置为完整,效率不高。

您应该尝试指定其他文件格式,我不知道您正在使用哪种格式,但 .PNG 用于小图片, .JPG 用于较大图片格式救命。 ( .GIF ,如果你真的需要额外的字节

在使用图像之前应该进行一些图像处理,但我猜你在这里没有选择。

答案 1 :(得分:2)

如上所述您的问题需要进行小改动尝试这个只需在您的控制器中调用此文件我希望它能为您提供帮助

 public class ImageDecrease
        {
            public static void Resize(Stream original, string output, int width, int height)
            {
                using (var image = Image.FromStream(original))
                using (var thumbnail = new Bitmap(width, height))
                using (var graphics = Graphics.FromImage(thumbnail))
                {
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;

                    graphics.DrawImage(image, 0, 0, width, height);

                    ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
                    EncoderParameters encoderParameters;
                    encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                    thumbnail.Save(output, info[1], encoderParameters);
                }
            }
        }