@@ -150,9 +150,6 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
150
150
this . SanitizeAndSetEncoderOptions < TPixel > ( this . encoder , pngMetadata , out this . use16Bit , out this . bytesPerPixel ) ;
151
151
152
152
stream . Write ( PngConstants . HeaderBytes ) ;
153
- this . WriteHeaderChunk ( stream ) ;
154
- this . WriteGammaChunk ( stream ) ;
155
- this . WriteColorProfileChunk ( stream , metadata ) ;
156
153
157
154
ImageFrame < TPixel > ? clonedFrame = null ;
158
155
ImageFrame < TPixel > currentFrame = image . Frames . RootFrame ;
@@ -164,9 +161,13 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
164
161
ClearTransparentPixels ( currentFrame ) ;
165
162
}
166
163
164
+ // Do not move this. We require an accurate bit depth for the header chunk.
167
165
IndexedImageFrame < TPixel > ? quantized = this . CreateQuantizedImageAndUpdateBitDepth ( pngMetadata , currentFrame , null ) ;
168
- this . WritePaletteChunk ( stream , quantized ) ;
169
166
167
+ this . WriteHeaderChunk ( stream ) ;
168
+ this . WriteGammaChunk ( stream ) ;
169
+ this . WriteColorProfileChunk ( stream , metadata ) ;
170
+ this . WritePaletteChunk ( stream , quantized ) ;
170
171
this . WriteTransparencyChunk ( stream , pngMetadata ) ;
171
172
this . WritePhysicalChunk ( stream , metadata ) ;
172
173
this . WriteExifChunk ( stream , metadata ) ;
0 commit comments