mirror of
https://github.com/quasar/Quasar.git
synced 2026-10-22 16:28:33 +08:00
af20dd54e3
Dispose of unmanaged resources used by the server's UnsafeStreamCodec and JpgCompression (because it is pretty related to the UnsafeStreamCodec). Made sure that the UnsafeStreamCodec was being disposed of before being re-assigned. Note: I did not reflect these changes to the client's UnsafeStreamCodec because the implementation of the client's codec is a bit different in the CommandHandler. This different implementation caused instability in the client. In the server, I noticed decreased memory consumption and decreased fluctuation in the memory used.
68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
|
|
namespace xServer.Core.Compression
|
|
{
|
|
public class JpgCompression : IDisposable
|
|
{
|
|
private readonly ImageCodecInfo _encoderInfo;
|
|
private readonly EncoderParameters _encoderParams;
|
|
|
|
public JpgCompression(long quality)
|
|
{
|
|
EncoderParameter parameter = new EncoderParameter(Encoder.Quality, quality);
|
|
this._encoderInfo = GetEncoderInfo("image/jpeg");
|
|
this._encoderParams = new EncoderParameters(2);
|
|
this._encoderParams.Param[0] = parameter;
|
|
this._encoderParams.Param[1] = new EncoderParameter(Encoder.Compression, (long) EncoderValue.CompressionRle);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
if (_encoderParams != null)
|
|
{
|
|
_encoderParams.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
public byte[] Compress(Bitmap bmp)
|
|
{
|
|
using (MemoryStream stream = new MemoryStream())
|
|
{
|
|
bmp.Save(stream, _encoderInfo, _encoderParams);
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
|
|
public void Compress(Bitmap bmp, ref Stream targetStream)
|
|
{
|
|
bmp.Save(targetStream, _encoderInfo, _encoderParams);
|
|
}
|
|
|
|
private ImageCodecInfo GetEncoderInfo(string mimeType)
|
|
{
|
|
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
|
|
int num2 = imageEncoders.Length - 1;
|
|
for (int i = 0; i <= num2; i++)
|
|
{
|
|
if (imageEncoders[i].MimeType == mimeType)
|
|
{
|
|
return imageEncoders[i];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |