From 3ca60efc9bd76574f7874ca4342eaff29aa532bd Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 22 Jan 2016 14:55:55 +1100 Subject: [PATCH] Fix memory leak in BitmapEncoder Former-commit-id: 064795cce7107001df5fd8adc610b7d8d19ef7dc Former-commit-id: d142c5bd63ad62e9307f80d42765d21d3a37e5e3 Former-commit-id: 9448861fae461c940d7fb493a44960c1f6238f13 --- src/ImageProcessor/Formats/Bmp/BmpEncoder.cs | 54 ++++++++++---------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/ImageProcessor/Formats/Bmp/BmpEncoder.cs b/src/ImageProcessor/Formats/Bmp/BmpEncoder.cs index b76b52055..3aef50836 100644 --- a/src/ImageProcessor/Formats/Bmp/BmpEncoder.cs +++ b/src/ImageProcessor/Formats/Bmp/BmpEncoder.cs @@ -51,35 +51,36 @@ namespace ImageProcessor.Formats rowWidth += 4 - amount; } - BinaryWriter writer = new BinaryWriter(stream); - - BmpFileHeader fileHeader = new BmpFileHeader + using (BinaryWriter writer = new BinaryWriter(stream)) { - Type = 19778, // BM - Offset = 54, - FileSize = 54 + (image.Height * rowWidth * 3) - }; + BmpFileHeader fileHeader = new BmpFileHeader + { + Type = 19778, // BM + Offset = 54, + FileSize = 54 + (image.Height * rowWidth * 3) + }; - WriteHeader(writer, fileHeader); + WriteHeader(writer, fileHeader); - BmpInfoHeader infoHeader = new BmpInfoHeader - { - HeaderSize = 40, - Height = image.Height, - Width = image.Width, - BitsPerPixel = 24, - Planes = 1, - Compression = BmpCompression.RGB, - ImageSize = image.Height * rowWidth * 3, - ClrUsed = 0, - ClrImportant = 0 - }; - - WriteInfo(writer, infoHeader); - - this.WriteImage(writer, image); - - writer.Flush(); + BmpInfoHeader infoHeader = new BmpInfoHeader + { + HeaderSize = 40, + Height = image.Height, + Width = image.Width, + BitsPerPixel = 24, + Planes = 1, + Compression = BmpCompression.RGB, + ImageSize = image.Height * rowWidth * 3, + ClrUsed = 0, + ClrImportant = 0 + }; + + WriteInfo(writer, infoHeader); + + this.WriteImage(writer, image); + + writer.Flush(); + } } /// @@ -100,6 +101,7 @@ namespace ImageProcessor.Formats amount = 4 - amount; } + // TODO: Make this parallel. for (int y = image.Height - 1; y >= 0; y--) { for (int x = 0; x < image.Width; x++)