'Itext 7 ImageDataFactory Leaves File Open?

I'm new to Stack Overflow so I apologize in advance for any stupid mistakes.

I am trying to add images to a PDF file using iText7. I have successfully done this with TIF, JPG, and PNG files, but I am running into a problem with GIF files.

It APPEARS that the ImageDataFactory.Create(FilePath) method is holding the source file open after returning.

My application is meant to delete each source image files after it has been added to the PDF file, so this behavior does not work for me. I have tried this code on several sample GIF files (from different sources) and all of them have the same problem, so I doubt that the specific file is to blame. I have looked for a method to close or release the ImageData and Image, but I can't identify anything that seems relevant.

I am appending a sample program here that is as simple as possible to demonstrate the problem. It manages to convert the JPG file, but not the GIF file. It throws a permission denied exception on the DeleteFile call.

This code is targeted at .NET 5.0 using Visual Studio 16.11.10 and iText 7.2.1

I assume that I am doing something wrong, but what?

I can upload the source image files, if necessary, but I'm not quite sure how to do that.

static void Main(string[] args)
        {
            Test("c:\\temp\\pdf\\12.jpg", "c:\\temp\\pdf\\12.pdf");
            Test("c:\\temp\\pdf\\13.gif", "c:\\temp\\pdf\\13.pdf");
        }

        static void Test(String TheSrcPath, String TheDstPath)
        {
            FileSystemObject MyFso;

            try
            {
                PdfDocument MyPdf;
                PdfWriter MyWriter;
                Document MyDoc;
                Image MyImage;
                ImageData MyImageData;

                MyWriter = new PdfWriter(TheDstPath);
                MyPdf = new PdfDocument(MyWriter);
                MyDoc = new Document(MyPdf);

                MyImageData = ImageDataFactory.Create(TheSrcPath);
                MyImage = new Image(MyImageData);

                MyDoc.Add(MyImage);

                MyDoc.Close();
                MyPdf.Close();
                MyWriter.Close();
                Console.WriteLine("Success creating: (" + TheDstPath + ")");
            }

            catch (Exception Ex)
            {
                Console.WriteLine("PDF Exception: (" + TheSrcPath + ")" + Ex.Message);
            }

            try
            {
                MyFso = new FileSystemObject();
                MyFso.DeleteFile(TheSrcPath);
                Console.WriteLine("Success deleting: (" + TheSrcPath + ")");
            }

            catch (Exception Ex)
            {
                Console.WriteLine("FSO Exception: (" + TheSrcPath + ")" + Ex.Message);
            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source