FIXME - improved pictureviewer should release file locks - now it doesn't show

This commit is contained in:
tomse
2025-08-23 17:20:41 +02:00
parent 626197a22b
commit 5d8f97e1b8

View File

@@ -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))) 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 try
{ {
string[] strOrigFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.OrigsDir), "*.tif*"); string[] strOrigFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.OrigsDir), "*.tif*");
if (File.Exists(strOrigFiles[0])) if (File.Exists(strOrigFiles[0]))
{ {
byte[] imageBytes = File.ReadAllBytes(strOrigFiles[0]); using (var memoryStream = new MemoryStream(File.ReadAllBytes(strOrigFiles[0])))
using (var bitmap = new Bitmap(memoryStream))
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{ {
using (Bitmap bitmap = new Bitmap(memoryStream)) // Create a copy of the bitmap to ensure the file is released
{ pictureBox1.Image = new Bitmap(bitmap);
pictureBox1.Image = new Bitmap(bitmap);
}
} }
// At this point, the file is fully released
} }
} }
catch catch
@@ -348,43 +347,49 @@ namespace PDFWorkflowManager
btnCopyPathWorkDir.Enabled = true; btnCopyPathWorkDir.Enabled = true;
btnConvertToPDF.Enabled = true; btnConvertToPDF.Enabled = true;
// Replace the selected code block with this version
try try
{ {
string[] strWorkFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkDir), "*.tif"); string[] strWorkFiles = Directory.GetFiles(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkDir), "*.tif");
byte[] imageBytes = File.ReadAllBytes(strWorkFiles[0]); if (strWorkFiles.Length > 0)
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{ {
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); pictureBox1.Image = new Bitmap(bitmap);
} }
// Release file handle so tempCopyPath is writeable
GC.Collect();
GC.WaitForPendingFinalizers();
} }
} }
catch catch
{ {
} }
if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir))) if (Directory.Exists(Path.Combine(txtProjectDir.Text, Properties.Settings.Default.WorkOutDir)))
{ {
try try
{ {
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");
byte[] imageBytes = File.ReadAllBytes(strOutFiles[0]); if (strOutFiles.Length > 0)
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{ {
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); pictureBox2.Image = new Bitmap(bitmap);
} }
// Release file handle so tempCopyPath is writeable
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(tempCopyPath);
} }
} }
catch catch
{ {
} }
} }
} }
@@ -1201,6 +1206,8 @@ namespace PDFWorkflowManager
bannerPage = ""; bannerPage = "";
} }
private void radioSortNormal_CheckedChanged(object sender, EventArgs e) private void radioSortNormal_CheckedChanged(object sender, EventArgs e)
{ {
//sortNormal = true; //sortNormal = true;
@@ -1557,5 +1564,27 @@ namespace PDFWorkflowManager
toolStripStatusLabel1.Text = ""; toolStripStatusLabel1.Text = "";
toolStripProgressBar1.Value = 0; 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
}
}
} }
} }