Exception creating and saving an image multiple times with ImageMagick.net

3.5k Views Asked by At

Perhaps I am wording that question poorly, but I am trying to follow the ImageMagick.net library documentation and I have a program that generates text based on user input.

It works great the first time, but program crashes subsequent times, whilst it is still running.

If I quit and relaunch it overwrites the previous file that is there without a hitch.

using (MagickImageCollection images = new MagickImageCollection())
{
    MagickReadSettings readSettings = new MagickReadSettings()
    {
        BackgroundColor = MagickColors.None, // -background none
        FillColor = MagickColors.Black, // -fill black
        Font = "Helvetica-Condensed-Light", // -font Helvetica-Condensed-Light
        FontPointsize = 26 // -pointsize 26
    };

    // this being declared a second time is probably why it crashes.

    MagickImage image = new MagickImage("label:" + output, readSettings);
    image.RemoveAttribute("label"); // +set label
    images.Add(image);

    MontageSettings montageSettings = new MontageSettings()
    {
        BackgroundColor = MagickColors.None, // -background none
        Shadow = true, // -shadow
        Geometry = new MagickGeometry(5, 5, 0, 0) // -geometry +5+5
    };

    using (MagickImage result = images.Montage(montageSettings))
    {
        result.Write("blarg.png");
    }
}

since the action of the above function resides in a button event handler, thus if pressed a second time, it will be declared a second time, but I am still relatively new to C# as well and unsure of a way to rewrite this properly since I am adapting it from their documentation.

Error:

Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q8-x64.dll
Exception type ImageMagick.MagickCoderErrorException
Exception message: WriteBlob Failed `blarg.png' @ error/png.c/MagickPNGErrorHandler/1650
Stack trace:    at ImageMagick.MagickExceptionHelper.Check(IntPtr exception)
  at ImageMagick.MagickImage.NativeMagickImage.WriteFile(MagickSettings settings)
  at phVer.frmMain.btnGenerate_Click(Object sender, EventArgs e) in C:\Users\jweinraub\documents\visual studio 2015\Projects\phVer\phVer\Form1.cs:line 83
---BEGIN InnerException--- 
Exception type ImageMagick.MagickBlobErrorException
Exception message: unable to open image 'blarg.png': Permission denied @   error/blob.c/OpenBlob/2695
Stack trace: 
---END Inner Exception

Update: It seems the actual problem is I have a preview pane that contains a background image of the image that gets saved. Subsequent runs crashes. Removing the preview pane allows the file to be written numerous times but unsure why preview pane has an issue (its a panel control with background image control). I've tried setting to null/disposing/&c to no avail.

/* Preview.BackgroundImage = Image.FromFile("blarg.png"); */
if ( Preview.BackgroundImage != null )
{
    Preview.BackgroundImage.Dispose();
    Preview.BackgroundImage = null;
    Preview.Dispose();
}
var image2 = (Bitmap)Image.FromFile("blarg.png", true);
Preview.BackgroundImage = image2;
1

There are 1 best solutions below

1
On BEST ANSWER

Image.FromFile will lock the file. You should do something like the example below to avoid the lock.

if (Preview.BackgroundImage != null)
  Preview.BackgroundImage.Dispose();

using (var bitmap = new Bitmap("blarg.png"))
{
  Preview.BackgroundImage = new Bitmap(bitmap);
}