fix output image not being loaded

This commit is contained in:
tomse
2026-02-01 14:34:25 +01:00
parent acd0304294
commit 990c103b39

View File

@@ -383,7 +383,7 @@ namespace PDFWorkflowManager
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show("Could not load preview image from Work directory." + ex, "Error loading preview image.", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Could not load preview image from Work directory." + ex.Message, "Error loading preview image.", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir))) if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir)))
@@ -393,23 +393,18 @@ namespace PDFWorkflowManager
string[] strOutFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir), "*.tif"); string[] strOutFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir), "*.tif");
if (strOutFiles.Length > 0) if (strOutFiles.Length > 0)
{ {
string tempCopyPath = Path.Combine(tempDir, "preview_out.tif"); using (var memoryStream = new MemoryStream(File.ReadAllBytes(strOutFiles[0])))
File.Copy(strOutFiles[0], tempCopyPath, true);
using (var memoryStream = new MemoryStream(File.ReadAllBytes(tempCopyPath)))
using (var bitmap = new Bitmap(memoryStream)) using (var bitmap = new Bitmap(memoryStream))
{ {
// Create a copy of the bitmap to ensure the file is released
pictureBox2.Image = new Bitmap(bitmap); pictureBox2.Image = new Bitmap(bitmap);
} }
// Release file handle so tempCopyPath is writeable // At this point, the file is fully released
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(tempCopyPath);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show("Could not load preview image from Work\\out directory." + ex, "Error loading preview image.", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Could not load preview image from Work\\out directory. " + ex.Message, "Error loading preview image.", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }