简体   繁体   English

如何在我的 ASP.NET Core Web 应用程序中命名多个上传的图像并为它们创建目录?

[英]How do I name multiple uploaded images and create directories for them in my ASP.NET Core Web Application?

My web application allows users to uploade multiple images for a product.我的 web 应用程序允许用户为一个产品上传多个图像。 The problem I have is that, I need to resize the uploaded images into specific widths and I also need to have them named accordingly.我遇到的问题是,我需要将上传的图像调整为特定的宽度,并且我还需要相应地命名它们。 The below action shows you that, any uplaoded image is processed and gives the output location, but it can only process one image.下面的动作显示,任何上传的图像都会被处理并给出 output 位置,但它只能处理一张图像。

wwwroot/images/products/[some id]/image700.jpg
wwwroot/images/products/[some id]/image350.jpg
wwwroot/images/products/[some id]/image150.jpg

So, what if there is more than one image?那么,如果有多个图像怎么办? I'm not sure how to handle that.我不确定如何处理。 Ideally I'd like something like:理想情况下,我想要类似的东西:

wwwroot/images/products/[some id]/image01-700.jpg
wwwroot/images/products/[some id]/image01-350.jpg
wwwroot/images/products/[some id]/image01-150.jpg

wwwroot/images/products/[some id]/image02-700.jpg
wwwroot/images/products/[some id]/image02-350.jpg
wwwroot/images/products/[some id]/image02-150.jpg

wwwroot/images/products/[some id]/image03-700.jpg
wwwroot/images/products/[some id]/image03-350.jpg
wwwroot/images/products/[some id]/image03-150.jpg

Here is my controller这是我的 controller

public Stream ProcessImages(Products model, int? id)
{
    /***************************
    *  These are the image sizes 
    *  I want to make from a single image
    ****************************/
    private readonly int[] sizeArray = new int[] { 700, 350, 150 };

    /***************************
    *  Start Process 
    ****************************/
    try
    {
        /***************************
        *  Define directories
        ****************************/  
        var images = model.ProductImages;
        var root = _env.WebRootPath;
        var folderName = Path.Combine(root, "images", "products", id.ToString());

        /***************************
        *  Create directory (none exists) 
        ****************************/
        if (!Directory.Exists(folderName))
        {
            Directory.CreateDirectory(folderName);
        }

        /***************************
        *  Iterate over the images
        ****************************/
        foreach (var item in images)
        {
            /***************************
            *  Iterate over the required
            *  sizes
            ****************************/
            foreach (var imageSize in sizeArray)
            {
                string imageSizeName = "image" + imageSize + ".jpg";
                string fullPath = Path.Combine(folderName, imageSizeName);
                
                //Create the stream and process the image
                using FileStream fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.ReadWrite);

                try
                {
                    Stream inStream = item.OpenReadStream();
                    using Image image = Image.Load(inStream);
                    int width = imageSize;
                    int height = 0;
                    var clone = image.Clone(i => i.Resize(width, height));
                    clone.SaveAsJpeg(fileStream);
                    inStream.Position = 0;
                }
                catch (Exception ex)
                {
                     Console.WriteLine(ex.Message);
                 }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return (null);
}

Here is the model:这是 model:

namespace MyProject.Data
{
    public class Products : BaseEntity
    {        
        public string Title { get; set; }        
        public IFormFileCollection ProductImages { get; set; }
        
    }
}

Does anyone know what I can do to process mulitple images in my situation?有谁知道在我的情况下我可以做些什么来处理多个图像?

for resizing the images you can use the following class要调整图像大小,您可以使用以下 class

    public class ImageResizer
    {
        public void Image_resize(string input_Image_Path, string output_Image_Path, int new_Width)
        {
            const long quality = 50L;
            Bitmap source_Bitmap = new Bitmap(input_Image_Path);
            double dblWidth_origial = source_Bitmap.Width;
            double dblHeigth_origial = source_Bitmap.Height;
            double relation_heigth_width = dblHeigth_origial / dblWidth_origial;
            int new_Height = (int)(new_Width * relation_heigth_width);
            var new_DrawArea = new Bitmap(new_Width, new_Height);
            using (var graphic_of_DrawArea = Graphics.FromImage(new_DrawArea))
            {
                graphic_of_DrawArea.CompositingQuality = CompositingQuality.HighSpeed;
                graphic_of_DrawArea.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphic_of_DrawArea.CompositingMode = CompositingMode.SourceCopy;
                graphic_of_DrawArea.DrawImage(source_Bitmap, 0, 0, new_Width, new_Height);
                using (var output = System.IO.File.Open(output_Image_Path, FileMode.Create))
                {
                    var qualityParamId = System.Drawing.Imaging.Encoder.Quality;
                    var encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality);
                    var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
                    new_DrawArea.Save(output, codec, encoderParameters);
                    output.Close();
                }
                graphic_of_DrawArea.Dispose();
            }
            source_Bitmap.Dispose();
        }
    }

the first argument defines where it can find the picture, the second one asks for a place to save the converted image and the last one asks you for picture width in pixles.第一个参数定义它可以在哪里找到图片,第二个参数要求保存转换后的图像的位置,最后一个参数要求您提供以像素为单位的图片宽度。 ( exactly what you wanted ) (正是你想要的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的ASP.NET Core Web应用程序创建WebJob - How to create a WebJob for my ASP.NET Core Web Application 启动ASP.NET Core应用程序后如何启动Web浏览器? - How do I launch the web browser after starting my ASP.NET Core application? 如何在 ASP.NET Core MVC 中创建具有多个图像的产品 - How to create product with multiple images in ASP.NET Core MVC 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? 在我的 asp.net 核心 MVC web 应用程序中,我如何使验证码成为必填字段 - Inside my asp.net core MVC web application how i can make the recaptcha a required field 如何在我的 ASP.NET Core 应用程序中显示和更新我的自定义标识字段? - How do I display and update my custom Identity fields in my ASP.NET Core application? 如何在 asp.net 核心 3.1 mvc web 应用程序中将日志保存到数据库? - How do I save logs to database in asp.net core 3.1 mvc web application? 如何在我上传文件的Asp.Net核心web api端点上进行集成测试? - How to do an integration test on my Asp.Net core web api endpoint where I upload a file? 如何使用 Entity Framework Core 和 ASP.NET Web API 创建工作列表并保存它们? - How can I create working lists and save them using the Entity Framework Core and ASP.NET Web API? 如何在我的 ASP.NET 核心应用程序中返回容器中的 blob 列表? - How do I return a list of blobs in a container within my ASP.NET Core application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM