Files
PulsarK-main/Pulsar.Common/Video/Compression/JpgCompression.cs
T
i2p 773d05f8f1
Pulsar .NET 9.0 Windows Release / build (push) Waiting to run
Mirror to Codeberg and Gitea / mirror (push) Waiting to run
initial commit
2026-08-27 10:57:58 -06:00

59 lines
1.9 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Pulsar.Common.Video.Compression
{
public class JpgCompression : IDisposable
{
private static readonly ImageCodecInfo JpegEncoderInfo = GetEncoderInfoStatic();
private readonly EncoderParameters _encoderParams;
public JpgCompression(long quality)
{
EncoderParameter parameter = new EncoderParameter(Encoder.Quality, quality);
_encoderParams = new EncoderParameters(1);
_encoderParams.Param[0] = parameter;
}
public void Dispose()
{
if (_encoderParams != null)
{
_encoderParams.Dispose();
}
GC.SuppressFinalize(this);
}
public byte[] Compress(Bitmap bmp)
{
if (bmp == null) throw new ArgumentNullException(nameof(bmp));
using (MemoryStream stream = new MemoryStream())
{
bmp.Save(stream, JpegEncoderInfo, _encoderParams);
return stream.ToArray();
}
}
public void Compress(Bitmap bmp, Stream targetStream)
{
if (bmp == null) throw new ArgumentNullException(nameof(bmp));
if (targetStream == null) throw new ArgumentNullException(nameof(targetStream));
bmp.Save(targetStream, JpegEncoderInfo, _encoderParams);
}
private static ImageCodecInfo GetEncoderInfoStatic()
{
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < imageEncoders.Length; i++)
{
if (imageEncoders[i].MimeType == "image/jpeg")
{
return imageEncoders[i];
}
}
throw new InvalidOperationException("JPEG encoder not found");
}
}
}