/// <summary>
/// Saves the JPEG image to the given stream. The caller is responsible for
/// disposing the stream.
/// </summary>
/// <param name="stream">The data stream used to save the image.</param>
public void Save(Stream stream)
{
// Write sections
foreach (JPEGSection section in Sections)
{
// Section header (including length bytes and section marker)
// must not exceed 64 kB.
if (section.Header.Length + 2 + 2 > 64 * 1024)
throw new SectionExceeds64KBException();
// Write section marker
stream.Write(new byte[] { 0xFF, (byte)section.Marker }, 0, 2);
// Write section header
if (section.Header.Length != 0)
{
// Header length including the length field itself
stream.Write(BitConverterEx.BigEndian.GetBytes((ushort)(section.Header.Length + 2)), 0, 2);
// Section header
stream.Write(section.Header, 0, section.Header.Length);
}
// Write entropy coded data
if (section.EntropyData.Length != 0)
stream.Write(section.EntropyData, 0, section.EntropyData.Length);
}
// Write trailing data, if any
if (TrailingData.Length != 0)
stream.Write(TrailingData, 0, TrailingData.Length);
}