Empty file when saving as PNG
<p>I’ve got a method which takes an input image, does some things to the image, then saves it to another file. At its most basic it resizes the image, but it can do a few more complicated things like converting to greyscale, quantizing etc, but for the case of this question I’m only attempting to resize the image and not performing any of the other actions.</p>

<p>It looks something like:</p>

<pre class="lang-csharp prettyprint-override"><code>public void SaveImage(string src, string dest, int width, int height, ImageFormat format, bool deleteOriginal, bool quantize, bool convertToGreyscale) {
    // Open the source file
    Bitmap source = (Bitmap)Image.FromFile(src);

    // Check dimensions
    if (source.Width &lt; width)
        throw new Exception();
    if (source.Height &lt; height)
        throw new Exception();

    // Output image
    Bitmap output = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(output)) {
        // Resize the image to new dimensions
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(source, 0, 0, width, height);
    }

    // Convert to greyscale if supposed to
    if (convertToGreyscale) {
        output = this.ConvertToGreyscale(output);
    }

    // Save the image
    if (quantize) {
        OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
        using (var quantized = quantizer.Quantize(output)) {
            quantized.Save(dest, format);
        }
    }
    else {
        output.Save(dest, format);
    }

    // Close all the images
    output.Dispose();
    source.Dispose();

    // Delete the original
    if (deleteOriginal) {
        File.Delete(src);
    }
}
</code></pre>

<p>Then to use it I’d call something like: <code>imageService.SaveImage("c:\image.png", "c:\output.png", 300, 300, ImageFormat.Png, false, false, false);</code></p>

<p>That should open up the “image.png” file, resize in to 300×300, then save it to “output.png” as a PNG file. But it’s not working—the file that gets created is in the correct location but has a file size of zero and contains no image at all.</p>

<p>This also only seems to happen when I pass in the parameter <code>ImageFormat.Png</code>; if I pass <code>ImageFormat.Jpeg</code>, then it works fine and creates the image file perfectly.</p>

<p>I’m wondering if there’s some sort of delay occurring between creating the image and somewhere else in the code attempting to access the image that’s been created (not in the above code) which locks up the file and so it’s never written to? Could that be the case?</p>

<p>Any ideas what else could be going on?</p>

<p>Cheers</p>

<p>Edits:</p>

<ul>
<li>remove redundant cast pointed out by Lloyd</li>
</ul>
