This project has moved and is read-only. For the latest updates, please go here.

How do I update a preview window from imagemagick?

Jan 3 at 9:55 PM
I am writing a program that takes a string from a textedit box, does imagemagick voodoo on it, but when it generates it, it saves the file as foo.png and a preview panel control loads foo.png. it works fine. however, if i stay in the program, change the text, and regenerate i get an unhandled exception that gets cross with me saying the file access is denied. the file itself still gets updated, it is just the panel control.

how do i fix it?
                    using (MagickImage result = images.Montage(montageSettings))

                catch (Exception ex)
                    string message = "Exception type " + ex.GetType() + Environment.NewLine + 
                        "Exception message: " + ex.Message + Environment.NewLine  + 
                        "Stack trace: " + ex.StackTrace + Environment.NewLine;

                    if (ex.InnerException != null)
                        message += "---BEGIN InnerException--- " + Environment.NewLine +
                                   "Exception type " + ex.InnerException.GetType() + Environment.NewLine +
                                   "Exception message: " + ex.InnerException.Message + Environment.NewLine +
                                   "Stack trace: " + ex.InnerException.StackTrace + Environment.NewLine +
                                   "---END Inner Exception";


            // problems start here mate
            // as if this gets commented out, it wont crash, but previewing is a necessity.
            Preview.BackgroundImage = Image.FromFile("foo.png");                          

            // End of problematic area
Jan 3 at 9:57 PM
Edited Jan 3 at 9:59 PM
This is the error thrown:
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q8-x64.dll
Exception type ImageMagick.MagickCoderErrorException
Exception message: WriteBlob Failed `foo.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 88
---BEGIN InnerException--- 
Exception type ImageMagick.MagickBlobErrorException
Exception message: unable to open image 'foo.png': Permission denied @ error/blob.c/OpenBlob/2695
Stack trace: 
---END Inner Exception
Line 88 is the result.write, but the file itself writes fine as i can go to explorer and see the changes went into effect, i cant reflect the changes in the panel control. how do i go by updating hte panel with the same file, as using a temp file, disposing, cloning, or increment a timestamp as a suffix seems like a cop out as there should be a more correct way of handling it (or at least more elegant)
Jan 3 at 11:17 PM
Image.FromFile will lock your file so you should do something like this:
if (Preview.BackgroundImage != null)

using (var bitmap = new Bitmap("foo.png"))
  Preview.BackgroundImage = new Bitmap(bitmap);