From 5d8f97e1b8bb2553668e3cb485087c33f72d2c58 Mon Sep 17 00:00:00 2001 From: tomse <1444539+tomse@users.noreply.github.com> Date: Sat, 23 Aug 2025 17:20:41 +0200 Subject: [PATCH] FIXME - improved pictureviewer should release file locks - now it doesn't show --- .../PDFWorkflowManager/MainForm.cs | 65 ++++++++++++++----- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/PDFWorkflowManager/PDFWorkflowManager/MainForm.cs b/PDFWorkflowManager/PDFWorkflowManager/MainForm.cs index c9ea5db..3bc3f33 100644 --- a/PDFWorkflowManager/PDFWorkflowManager/MainForm.cs +++ b/PDFWorkflowManager/PDFWorkflowManager/MainForm.cs @@ -322,20 +322,19 @@ namespace PDFWorkflowManager if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.OrigsDir)) && !Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkDir))) { + // Replace the selected code block with this version try { string[] strOrigFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.OrigsDir), "*.tif*"); if (File.Exists(strOrigFiles[0])) { - byte[] imageBytes = File.ReadAllBytes(strOrigFiles[0]); - - using (MemoryStream memoryStream = new MemoryStream(imageBytes)) + using (var memoryStream = new MemoryStream(File.ReadAllBytes(strOrigFiles[0]))) + using (var bitmap = new Bitmap(memoryStream)) { - using (Bitmap bitmap = new Bitmap(memoryStream)) - { - pictureBox1.Image = new Bitmap(bitmap); - } + // Create a copy of the bitmap to ensure the file is released + pictureBox1.Image = new Bitmap(bitmap); } + // At this point, the file is fully released } } catch @@ -348,43 +347,49 @@ namespace PDFWorkflowManager btnCopyPathWorkDir.Enabled = true; btnConvertToPDF.Enabled = true; + // Replace the selected code block with this version try { string[] strWorkFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkDir), "*.tif"); - byte[] imageBytes = File.ReadAllBytes(strWorkFiles[0]); - - using (MemoryStream memoryStream = new MemoryStream(imageBytes)) + if (strWorkFiles.Length > 0) { - using (Bitmap bitmap = new Bitmap(memoryStream)) + using (var memoryStream = new MemoryStream(File.ReadAllBytes(strWorkFiles[0]))) + using (var bitmap = new Bitmap(memoryStream)) { pictureBox1.Image = new Bitmap(bitmap); } + // Release file handle so tempCopyPath is writeable + GC.Collect(); + GC.WaitForPendingFinalizers(); } } catch { - } - if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir))) { try { string[] strOutFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir), "*.tif"); - byte[] imageBytes = File.ReadAllBytes(strOutFiles[0]); - - using (MemoryStream memoryStream = new MemoryStream(imageBytes)) + if (strOutFiles.Length > 0) { - using (Bitmap bitmap = new Bitmap(memoryStream)) + string tempCopyPath = Path.Combine(tempDir, "preview_out.tif"); + File.Copy(strOutFiles[0], tempCopyPath, true); + + using (var memoryStream = new MemoryStream(File.ReadAllBytes(tempCopyPath))) + using (var bitmap = new Bitmap(memoryStream)) { pictureBox2.Image = new Bitmap(bitmap); } + // Release file handle so tempCopyPath is writeable + GC.Collect(); + GC.WaitForPendingFinalizers(); + File.Delete(tempCopyPath); } } catch { - } } } @@ -1201,6 +1206,8 @@ namespace PDFWorkflowManager bannerPage = ""; } + + private void radioSortNormal_CheckedChanged(object sender, EventArgs e) { //sortNormal = true; @@ -1557,5 +1564,27 @@ namespace PDFWorkflowManager toolStripStatusLabel1.Text = ""; toolStripProgressBar1.Value = 0; } + private void HandleWorkOutDirFileEvent() + { + try + { + string[] strOutFiles = Directory.GetFiles(workOutDir, "*.tif"); + if (strOutFiles.Length > 0) + { + using (var memoryStream = new MemoryStream(File.ReadAllBytes(strOutFiles[0]))) + using (var bitmap = new Bitmap(memoryStream)) + { + pictureBox2.Image = new Bitmap(bitmap); + } + // Release file handle so file is writeable + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } + catch + { + // Optionally log or handle error + } + } } }