ASP.NET Core 3.1 图片压缩

huangapple go评论91阅读模式
英文:

ASP.NET Core 3.1 Images Compress

问题

有没有在ASP.NET Core 3.1 MVC中压缩图像文件(.jpg/.png)并创建缩略图的免费可靠方法?

我尝试了Bitmap的函数,但它们抛出了异常,如“此平台不支持System.Drawing.Common”。

英文:

Is there any free and reliable way to compress image files (.jpg/.png) and also create thumbnails in ASP.NET Core 3.1 MVC?

I tried Bitmap's functions but they throw exceptions like 'System.Drawing.Common is not supported on this platform'.

答案1

得分: 1

  1. ImageSharp是一款高性能的、跨平台的.NET Core图像处理库。它是System.Drawing.Common的现代替代品。

  2. 支持多种图像格式并优化性能,它是处理与图像相关任务的出色选择,适用于ASP.NET Core和其他.NET Core项目。

  3. 通过集成SixLabors.ImageSharp NuGet包,您可以轻松地以跨平台的方式读取、写入、调整大小和执行其他图像操作。

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// 从文件或流加载图像
using (Image image = Image.Load("path/to/your/image.jpg"))
{
    // 将图像调整为特定的宽度和高度
    int newWidth = 300;
    int newHeight = 200;
    image.Mutate(x => x.Resize(newWidth, newHeight));

    // 将调整大小后的图像保存到文件或流
    image.Save("path/to/your/resized_image.jpg");
}
英文:
  1. ImageSharp is a high-performance, cross-platform image processing
    library for .NET Core. It serves as a modern alternative to
    System.Drawing.Common.

  2. With support for multiple image formats and optimized performance,
    it is an excellent choice for handling image-related tasks in
    ASP.NET Core and other .NET Core projects.

  3. By integrating the SixLabors.ImageSharp NuGet package, you can
    easily read, write, resize, and perform other image operations in a
    cross-platform manner.

    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.Processing;
    
    // Load the image from a file or stream
    using (Image image = Image.Load("path/to/your/image.jpg"))
    {
        // Resize the image to a specific width and height
        int newWidth = 300;
        int newHeight = 200;
        image.Mutate(x => x.Resize(newWidth, newHeight));
    
        // Save the resized image to a file or stream
        image.Save("path/to/your/resized_image.jpg");
    }
    

答案2

得分: 0

是的,在ASP.NET Core中,有免费可靠的方法来压缩图像文件并创建缩略图,而无需使用System.Drawing.Common库,因为该库在ASP.NET Core中由于平台限制而不受支持。以下是两个常用且广泛使用的库,您可以使用它们:

ImageSharp(SixLabors.ImageSharp):
ImageSharp是一个跨平台的高性能图像处理库,适用于.NET,可用于ASP.NET Core应用程序。它提供了丰富的图像处理功能,包括调整大小、裁剪、压缩等。
要在ASP.NET Core 3.1 MVC应用程序中使用ImageSharp,您需要安装SixLabors.ImageSharp.Web NuGet包。

Install-Package SixLabors.ImageSharp.Web

以下是使用ImageSharp压缩图像并生成缩略图的示例代码:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

public void ProcessImage(string inputFilePath, string outputFilePath, int maxWidth, int maxHeight)
{
    using (var image = Image.Load(inputFilePath))
    {
        // 调整图像大小以创建缩略图
        image.Mutate(x => x.Resize(maxWidth, maxHeight));

        // 使用指定的质量级别(例如,70)压缩图像
        var encoder = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder { Quality = 70 };

        // 保存缩略图到输出文件
        image.Save(outputFilePath, encoder);
    }
}
英文:

Yes, there are free and reliable ways to compress image files and create thumbnails in ASP.NET Core without using the System.Drawing.Common library, which is not supported in ASP.NET Core due to platform limitations. Here are two popular and widely used libraries that you can use:

ImageSharp (SixLabors.ImageSharp):
ImageSharp is a cross-platform, high-performance image processing library for .NET that can be used in ASP.NET Core applications. It provides a rich set of features for image manipulation, including resizing, cropping, compression, and more.
To use ImageSharp in your ASP.NET Core 3.1 MVC application, you need to install the SixLabors.ImageSharp.Web NuGet package.

> Install-Package SixLabors.ImageSharp.Web

Here's a sample code snippet to compress an image and generate a thumbnail using ImageSharp:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

public void ProcessImage(string inputFilePath, string outputFilePath, int maxWidth, int maxHeight)
{
    using (var image = Image.Load(inputFilePath))
    {
        // Resize the image to create a thumbnail
        image.Mutate(x => x.Resize(maxWidth, maxHeight));

        // Compress the image with the specified quality level (e.g., 70)
        var encoder = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder { Quality = 70 };

        // Save the thumbnail to the output file
        image.Save(outputFilePath, encoder);
    }
}

huangapple
  • 本文由 发表于 2023年7月31日 19:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定