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

相关推荐
小龙报几秒前
《算法通关指南之C++编程篇(5)----- 条件判断与循环(下)》
c语言·开发语言·c++·算法·visualstudio·学习方法·visual studio
郝学胜-神的一滴3 分钟前
C++ STL(标准模板库)深度解析:从基础到实践
linux·服务器·开发语言·c++·算法
LL_break7 分钟前
线程3 JavaEE(阻塞队列,线程池)
java·开发语言·java-ee·线程·线程池·阻塞队列
Fortunate Chen12 分钟前
初识C语言12. 结构体(自定义类型的核心工具)
c语言·开发语言·笔记
刚入坑的新人编程27 分钟前
算法训练.17
开发语言·数据结构·c++·算法
汤姆yu29 分钟前
基于python大数据深度学习的酒店评论文本情感分析
开发语言·python·深度学习
狂团商城小师妹35 分钟前
JAVA无人共享台球杆台球柜系统球杆柜租赁系统源码支持微信小程序
java·开发语言·微信小程序·小程序
Fortunate Chen38 分钟前
初识C语言13.自定义类型(联合体与枚举)
c语言·开发语言
麦麦鸡腿堡38 分钟前
Java的抽象类实践-模板设计模式
java·开发语言·设计模式
shandianchengzi40 分钟前
【记录】Unity|Unity从安装到打开一个Github项目(以我的世界(仿)为例)
unity·c#·游戏引擎·github·我的世界·mc