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)))
{
// 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
}
}
}
}