diff --git a/GreenshotPlugin/Core/ImageOutput.cs b/GreenshotPlugin/Core/ImageOutput.cs
index 72a5fdbf4..fd38a6b24 100644
--- a/GreenshotPlugin/Core/ImageOutput.cs
+++ b/GreenshotPlugin/Core/ImageOutput.cs
@@ -259,21 +259,22 @@ namespace GreenshotPlugin.Core {
// check for color reduction, forced or automatically, only when the DisableReduceColors is false
if (!outputSettings.DisableReduceColors && (conf.OutputFileAutoReduceColors || outputSettings.ReduceColors)) {
- WuQuantizer quantizer = new WuQuantizer((Bitmap)imageToSave);
- int colorCount = quantizer.GetColorCount();
- LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
- if (outputSettings.ReduceColors || colorCount < 256) {
- try {
- LOG.Info("Reducing colors on bitmap to 256.");
- tmpImage = quantizer.GetQuantizedImage(256);
- if (disposeImage) {
- imageToSave.Dispose();
+ using (WuQuantizer quantizer = new WuQuantizer((Bitmap)imageToSave)) {
+ int colorCount = quantizer.GetColorCount();
+ LOG.InfoFormat("Image with format {0} has {1} colors", imageToSave.PixelFormat, colorCount);
+ if (outputSettings.ReduceColors || colorCount < 256) {
+ try {
+ LOG.Info("Reducing colors on bitmap to 256.");
+ tmpImage = quantizer.GetQuantizedImage(256);
+ if (disposeImage) {
+ imageToSave.Dispose();
+ }
+ imageToSave = tmpImage;
+ // Make sure the "new" image is disposed
+ disposeImage = true;
+ } catch (Exception e) {
+ LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
}
- imageToSave = tmpImage;
- // Make sure the "new" image is disposed
- disposeImage = true;
- } catch (Exception e) {
- LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
}
}
}
diff --git a/GreenshotPlugin/Core/QuantizerHelper.cs b/GreenshotPlugin/Core/QuantizerHelper.cs
index bb2129e01..9af593bb9 100644
--- a/GreenshotPlugin/Core/QuantizerHelper.cs
+++ b/GreenshotPlugin/Core/QuantizerHelper.cs
@@ -69,7 +69,7 @@ namespace GreenshotPlugin.Core {
public Int32 Volume { get; set; }
}
- public class WuQuantizer {
+ public class WuQuantizer : IDisposable {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(WuQuantizer));
private const Int32 MAXCOLOR = 512;
@@ -100,6 +100,13 @@ namespace GreenshotPlugin.Core {
private Bitmap sourceBitmap;
private Bitmap resultBitmap;
+ public void Dispose() {
+ if (resultBitmap != null) {
+ resultBitmap.Dispose();
+ resultBitmap = null;
+ }
+ }
+
///
/// See for more details.
///
@@ -222,7 +229,11 @@ namespace GreenshotPlugin.Core {
}
}
resultBitmap.Palette = imagePalette;
- return resultBitmap;
+
+ // Make sure the bitmap is not disposed, as we return it.
+ Bitmap tmpBitmap = resultBitmap;
+ resultBitmap = null;
+ return tmpBitmap;
}
///
@@ -361,7 +372,11 @@ namespace GreenshotPlugin.Core {
imagePalette.Entries[paletteIndex] = Color.FromArgb(255, reds[paletteIndex], greens[paletteIndex], blues[paletteIndex]);
}
resultBitmap.Palette = imagePalette;
- return resultBitmap;
+
+ // Make sure the bitmap is not disposed, as we return it.
+ Bitmap tmpBitmap = resultBitmap;
+ resultBitmap = null;
+ return tmpBitmap;
}
///
@@ -632,5 +647,5 @@ namespace GreenshotPlugin.Core {
}
}
}
- }
+ }
}
\ No newline at end of file