blob: 74484997728dd39f8caad2760e6aaf53976cde45 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.RemoteDesktop.Encoders
{
/// <summary>
/// Represents an <see cref="IFrame"/> JPEG encoder.
/// </summary>
/// <seealso cref="Tango.RemoteDesktop.FrameEncoder" />
public class JpegEncoder : FrameEncoder
{
/// <summary>
/// Initializes a new instance of the <see cref="JpegEncoder"/> class.
/// </summary>
/// <param name="frame">The frame.</param>
public JpegEncoder(IFrame frame) : base(frame)
{
}
/// <summary>
/// Returns a stream containing the encoded frame.
/// </summary>
/// <returns></returns>
public override MemoryStream ToStream()
{
return ToStream(100);
}
/// <summary>
/// Returns a stream containing the encoded frame with the specified quality.
/// </summary>
/// <param name="quality">The quality.</param>
/// <returns></returns>
public virtual MemoryStream ToStream(long quality)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
MemoryStream ms = new MemoryStream();
Frame.ToBitmap().Save(ms, GetEncoder(ImageFormat.Jpeg), encoderParameters);
ms.Position = 0;
return ms;
}
/// <summary>
/// Returns a byte array containing the encoded frame with the specified quality.
/// </summary>
/// <param name="quality">The quality.</param>
/// <returns></returns>
public byte[] ToArray(long quality)
{
using (MemoryStream ms = ToStream(quality))
{
return ms.ToArray();
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
return codecs.Single(codec => codec.FormatID == format.Guid);
}
}
}
|