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

相关推荐
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题1 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法