From 860c34b8bb1b97eb4ff8cc2be688952bdb110056 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 10 Nov 2016 17:45:41 +1100 Subject: [PATCH] Flush only when you need to. --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 2a4683e70..8e83d477e 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -591,7 +591,8 @@ namespace ImageSharp.Formats int bytesPerScanline = this.width * this.bytesPerPixel; byte[] previousScanline = ArrayPool.Shared.Rent(bytesPerScanline); byte[] rawScanline = ArrayPool.Shared.Rent(bytesPerScanline); - byte[] result = ArrayPool.Shared.Rent(bytesPerScanline + 1); + int resultLength = bytesPerScanline + 1; + byte[] result = ArrayPool.Shared.Rent(resultLength); byte[] buffer; int bufferLength; @@ -604,8 +605,7 @@ namespace ImageSharp.Formats for (int y = 0; y < this.height; y++) { this.EncodePixelRow(pixels, y, previousScanline, rawScanline, result, bytesPerScanline); - deflateStream.Write(result, 0, bytesPerScanline + 1); - deflateStream.Flush(); + deflateStream.Write(result, 0, resultLength); // Do a bit of shuffling; byte[] tmp = rawScanline; @@ -613,6 +613,7 @@ namespace ImageSharp.Formats previousScanline = tmp; } + deflateStream.Flush(); bufferLength = (int)memoryStream.Length; buffer = memoryStream.ToArray(); }