使用 C# 压缩 PNG、JPEG 和 TIFF 图像

图像压缩是一种常用的减小图像大小的方法。它可以让您分别最小化存储和传输图像所需的空间和时间。有各种压缩技术可用来压缩图像,而不会降低图像的质量。根据这一点,这篇文章为您提供了一些使用 C# 以编程方式 **压缩PNGJPEGTIFF**图像的简单方法。

C# 图像压缩 API - 免费下载

Aspose.Imaging for .NET是一款功能强大的图像处理 API,可让您在 .NET 应用程序中处理常见的图像格式。此外,该 API 还允许您对图像应用不同类型的压缩,包括 PNG、JPEG 和 TIFF。要使用该 API,您可以下载其 DLL 或使用NuGet安装它。

cs 复制代码
Install-Package Aspose.Imaging

在 C# 中压缩 PNG 图像

对于 PNG 图像,您可以将压缩级别设置为 0 到 9,其中 9 是最大压缩,0 是存储模式。以下是使用 Aspose.Imaging for .NET 压缩 PNG 图像的步骤。

以下代码示例展示如何使用 C# 压缩 PNG 图像。

cs 复制代码
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_PNG();
 
// Load an image from file (or stream)
using (Image image = Image.Load(dataDir + "aspose_logo.png"))
{
    // Loop over possible CompressionLevel range
    for (int i = 0; i <= 9; i++)
    {
        // Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and  Save result on disk
        PngOptions options = new PngOptions();
        options.CompressionLevel = i;
        image.Save(i + "_out.png", options);
    }
}

在 C# 中压缩 JPEG 图像

为了处理 JPEG 图像,Aspose.Imaging for .NET 提供了JpegOptions类,该类为 JPEG 图像提供以下压缩类型。

  • 基线
  • 进步
  • 无损
  • JPEG格式

以下是使用上述压缩类型之一压缩 JPEG 图像的步骤。

以下代码示例展示如何使用 C# 压缩 JPEG 图像。

cs 复制代码
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_JPEG();
          
using (var original = Image.Load(dataDir+"ColorGif.gif"))
{
    var jpegOptions = new JpegOptions()
{
    ColorType = JpegCompressionColorMode.Grayscale,
    CompressionType = JpegCompressionMode.Progressive,
          
};
    original.Save("D:/temp/result.jpg", jpegOptions);
}

在 C# 中对 TIFF 图像应用压缩

Aspose.Imaging for .NET 为 TIFF 图像提供了多种压缩类型,包括 LZW、Packbits、CCIT Fax 3 & 4 等。您可以根据需要选择适当的类型。以下是压缩 TIFF 图像的步骤。

以下代码示例展示如何使用 C# 压缩 TIFF 图像。

cs 复制代码
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_ModifyingAndConvertingImages();

// Load an image through file path location or stream
Image image = Image.Load(dataDir + "SampleTiff.tiff");

// Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions(TiffExpectedFormat.Default);

// Set BitsPerSample, Compression, Photometric mode and graycale palette
outputSettings.BitsPerSample = new ushort[] { 4 };
outputSettings.Compression = TiffCompressions.Lzw;
outputSettings.Photometric = TiffPhotometrics.Palette;
outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false);
image.Save(dataDir + "SampleTiff_out.tiff", outputSettings);

结论

在本文中,您学习了如何使用 C# 压缩 PNG、JPEG 和 TIFF 图像。还列出了 JPEG 和 TIFF 图像支持的各种压缩技术。您可以使用文档了解有关 .NET 图像处理 API 的更多信息。

相关推荐
四维碎片3 小时前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio
IT码农-爱吃辣条3 小时前
Three.js 初级教程大全
开发语言·javascript·three.js
☺����3 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
染翰4 小时前
lua入门以及在Redis中的应用
开发语言·redis·lua
王者鳜錸4 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化
妮妮学代码4 小时前
c#:TCP服务端管理类
java·tcp/ip·c#
兔老大RabbitMQ4 小时前
git pull origin master失败
java·开发语言·git
tt5555555555555 小时前
C/C++嵌入式笔试核心考点精解
c语言·开发语言·c++
xiao助阵5 小时前
python实现梅尔频率倒谱系数(MFCC) 除了傅里叶变换和离散余弦变换
开发语言·python
科大饭桶5 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器