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

相关推荐
WineMonk30 分钟前
ArcPy 与 ArcGIS .NET SDK 读取 GDB 要素类坐标系失败?GDAL 外挂方案详解
arcgis·c#·.net·arcgispro
界面开发小八哥31 分钟前
界面开发框架DevExpress XAF实践:集成.NET Aspire后如何实现服务安排?
c#·.net·界面控件·devexpress·ui开发·xaf
fs哆哆33 分钟前
在VB.net中,用正则表达式方法清除干扰符号方法
开发语言·正则表达式·c#·.net
嵌入式@秋刀鱼1 小时前
《 第三章-招式初成》 C++修炼生涯笔记(基础篇)程序流程结构
linux·开发语言·数据结构·c++·笔记·visual studio code
shenyan~1 小时前
关于 WASM: WASM + JS 混合逆向流程
开发语言·javascript·wasm
梦境虽美,却不长1 小时前
C语言 学习 文件操作(开关,读写,定位,大小)操作 2025年6月8日12:19:24
c语言·开发语言·学习
Charlotte_jc1 小时前
完美解决openpyxl保存Excel丢失图像/形状资源的技术方案
开发语言·python·excel·openpyxl
西北大程序猿2 小时前
服务器代码知识点补充
服务器·开发语言·网络·c++·网络协议
新知图书4 小时前
R语言ICU患者死亡率预测实战
开发语言·r语言
蒟蒻小袁4 小时前
力扣面试150题--实现Trie(前缀树)
leetcode·面试·c#