本快速教程分享了有关如何在 C# 中压缩图像大小的信息。在 C# compress image 中演示的几行代码,例如 JPEG、PNG、BMP 等任何类型,这样您就可以完全控制压缩过程。为此,提供了不同的属性来压缩图像而不损失质量。
在 C# 中压缩图像大小的步骤
- 将项目配置为使用 Aspose.Imaging 压缩 JPEG 图像
- 将源 JPEG 文件加载到 Image 类对象中
- 创建并初始化 JpegOptions 类对象以自定义压缩
- 将颜色类型设置为灰度以减小图像尺寸
- 将压缩类型设置为渐进式
- 使用上面给出的压缩设置将源图像保存在磁盘上
压缩图像大小 C# 环境配置和操作顺序在上述步骤中进行了描述。识别所有必要的类,就像将源图像加载到 Image 类中一样。类似地,JpegOptions 类对象用于配置输出压缩图像以及示例代码中设置的几个属性的使用。
在 C# 中压缩图像大小的代码
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
namespace CompressImageSizeInCSharpInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to compress Jpeg image in CSharp | |
{ | |
// Load the license to compress JPEG image | |
Aspose.Imaging.License licCompressJpeg= new Aspose.Imaging.License(); | |
licCompressJpeg.SetLicense("Aspose.Imaging.lic"); | |
// Load the original image to be compressed | |
using (var originalJpegImage = Image.Load("SampleJpeg.jpg")) | |
{ | |
// Create JpegOptions class object to customize output image | |
var jpegOptionsToCompress = new Aspose.Imaging.ImageOptions.JpegOptions() | |
{ | |
// Set color type to Grayscale to reduce size | |
ColorType = JpegCompressionColorMode.Grayscale, | |
// Set compression type to progressive | |
CompressionType = JpegCompressionMode.Progressive, | |
}; | |
// Save the output compressed image on the disk | |
originalJpegImage.Save("result.jpg", jpegOptionsToCompress); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
C# 中的上述程序语句使用 JpegOptions 压缩图像大小,其中颜色类型设置为灰度,压缩类型设置为渐进。还有许多其他属性可用,例如调色板、质量、全帧标志、多页选项、水平采样和垂直采样等等。如果要压缩 PNG 图像,可以使用 PNGOptions,而对于 TIFF 图像,请使用 TiffOptions。
在本教程中,我们学习了压缩 C# 中的图像大小 如果要调整图像大小,请参阅 如何在 C# 中调整图像大小 上的文章。