@@ -167,6 +167,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
167
167
168
168
ImageFrame < TPixel > ? clonedFrame = null ;
169
169
ImageFrame < TPixel > currentFrame = image . Frames . RootFrame ;
170
+ int currentFrameIndex = 0 ;
170
171
171
172
bool clearTransparency = this . encoder . TransparentColorMode is PngTransparentColorMode . Clear ;
172
173
if ( clearTransparency )
@@ -196,28 +197,49 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
196
197
if ( image . Frames . Count > 1 )
197
198
{
198
199
this . WriteAnimationControlChunk ( stream , ( uint ) image . Frames . Count , pngMetadata . RepeatCount ) ;
200
+ }
201
+
202
+ // If the first frame isn't animated, write it as usual and skip it when writing animated frames
203
+ if ( ! pngMetadata . DefaultImageAnimated || image . Frames . Count == 1 )
204
+ {
205
+ FrameControl frameControl = new ( ( uint ) this . width , ( uint ) this . height ) ;
206
+ this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , false ) ;
207
+ currentFrameIndex ++ ;
208
+ }
199
209
200
- // Write the first frame.
210
+ if ( image . Frames . Count > 1 )
211
+ {
212
+ // Write the first animated frame.
213
+ currentFrame = image . Frames [ currentFrameIndex ] ;
201
214
PngFrameMetadata frameMetadata = GetPngFrameMetadata ( currentFrame ) ;
202
215
PngDisposalMethod previousDisposal = frameMetadata . DisposalMethod ;
203
216
FrameControl frameControl = this . WriteFrameControlChunk ( stream , frameMetadata , currentFrame . Bounds ( ) , 0 ) ;
204
- this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , false ) ;
217
+ uint sequenceNumber = 1 ;
218
+ if ( pngMetadata . DefaultImageAnimated )
219
+ {
220
+ this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , false ) ;
221
+ }
222
+ else
223
+ {
224
+ sequenceNumber += this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , true ) ;
225
+ }
226
+
227
+ currentFrameIndex ++ ;
205
228
206
229
// Capture the global palette for reuse on subsequent frames.
207
230
ReadOnlyMemory < TPixel > ? previousPalette = quantized ? . Palette . ToArray ( ) ;
208
231
209
232
// Write following frames.
210
- uint increment = 0 ;
211
233
ImageFrame < TPixel > previousFrame = image . Frames . RootFrame ;
212
234
213
235
// This frame is reused to store de-duplicated pixel buffers.
214
236
using ImageFrame < TPixel > encodingFrame = new ( image . Configuration , previousFrame . Size ( ) ) ;
215
237
216
- for ( int i = 1 ; i < image . Frames . Count ; i ++ )
238
+ for ( ; currentFrameIndex < image . Frames . Count ; currentFrameIndex ++ )
217
239
{
218
240
ImageFrame < TPixel > ? prev = previousDisposal == PngDisposalMethod . RestoreToBackground ? null : previousFrame ;
219
- currentFrame = image . Frames [ i ] ;
220
- ImageFrame < TPixel > ? nextFrame = i < image . Frames . Count - 1 ? image . Frames [ i + 1 ] : null ;
241
+ currentFrame = image . Frames [ currentFrameIndex ] ;
242
+ ImageFrame < TPixel > ? nextFrame = currentFrameIndex < image . Frames . Count - 1 ? image . Frames [ currentFrameIndex + 1 ] : null ;
221
243
222
244
frameMetadata = GetPngFrameMetadata ( currentFrame ) ;
223
245
bool blend = frameMetadata . BlendMethod == PngBlendMethod . Over ;
@@ -238,22 +260,17 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
238
260
}
239
261
240
262
// Each frame control sequence number must be incremented by the number of frame data chunks that follow.
241
- frameControl = this . WriteFrameControlChunk ( stream , frameMetadata , bounds , ( uint ) i + increment ) ;
263
+ frameControl = this . WriteFrameControlChunk ( stream , frameMetadata , bounds , sequenceNumber ) ;
242
264
243
265
// Dispose of previous quantized frame and reassign.
244
266
quantized ? . Dispose ( ) ;
245
267
quantized = this . CreateQuantizedImageAndUpdateBitDepth ( pngMetadata , encodingFrame , bounds , previousPalette ) ;
246
- increment += this . WriteDataChunks ( frameControl , encodingFrame . PixelBuffer . GetRegion ( bounds ) , quantized , stream , true ) ;
268
+ sequenceNumber += this . WriteDataChunks ( frameControl , encodingFrame . PixelBuffer . GetRegion ( bounds ) , quantized , stream , true ) + 1 ;
247
269
248
270
previousFrame = currentFrame ;
249
271
previousDisposal = frameMetadata . DisposalMethod ;
250
272
}
251
273
}
252
- else
253
- {
254
- FrameControl frameControl = new ( ( uint ) this . width , ( uint ) this . height ) ;
255
- this . WriteDataChunks ( frameControl , currentFrame . PixelBuffer . GetRegion ( ) , quantized , stream , false ) ;
256
- }
257
274
258
275
this . WriteEndChunk ( stream ) ;
259
276
0 commit comments