使用 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 的更多信息。

相关推荐
向阳@向远方6 分钟前
第二章 简单程序设计
开发语言·c++·算法
Mr_Xuhhh37 分钟前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
纳兰青华1 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
好开心啊没烦恼1 小时前
Python 数据分析:DataFrame,生成,用字典创建 DataFrame ,键值对数量不一样怎么办?
开发语言·python·数据挖掘·数据分析
liulilittle1 小时前
VGW 虚拟网关用户手册 (PPP PRIVATE NETWORK 基础设施)
开发语言·网络·c++·网关·智能路由器·路由器·通信
Devil枫1 小时前
Kotlin高级特性深度解析
android·开发语言·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.1.20 的新特性
android·开发语言·kotlin
安之若素^1 小时前
启用不安全的HTTP方法
java·开发语言
一个天蝎座 白勺 程序猿2 小时前
Python(28)Python循环语句指南:从语法糖到CPython字节码的底层探秘
开发语言·python
持梦远方2 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++