Handling Multipage image files error

saljas
saljas used Ask the Experts™
on
(Object reference not set to an instance of an object)

The following code (sometimes) give the above error only if the file is a (multi page tiff file with a large number of pages)  e.g.  400 pages or 500 pages. Not on a specific page.
and when I check     PictureBox1.Image     I find it = null!
If I reRun my software it works fine on the same page that it stopped on.
I feel it is something related to memory   (e.g.   executing the next line before full load .....)
Any help is appreciated.

PictureBox1.Image = Image.FromFile("C:\ImgFolder\ImageName.tif")
Dim NoOfPages As Long = PictureBox1.Image.GetFrameCount(Imaging.FrameDimension.Page)

For J = ImagePageFrom - 1 To ImagePageTO - 1
       PictureBox1.Image.SelectActiveFrame(Imaging.FrameDimension.Page, J)    'the error is on this line
       PictureBox1.Image.Save("C:\ImgFolder\ImageName.Gif", Imaging.ImageFormat.Gif)
Next
PictureBox1.Image = Nothing
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi saljas,

You just need to extract the images from Tiff? or do you have to display it as well?

Regards,
Chinmay.

Author

Commented:
Many thanks for your help Mr. Chinmay,

Extraction:-
This code is working fine for millions of tiff images, (Sometimes) - (not always) it gives error on large files.
Because the image file is large (I am assuming)      PictureBox1.Image     unexpectedly becomes = null    then the error appears
Any way, if possible please explain how to extract it.
Or if there is a way to check that the image load is complete.

Displaying The Image:-
No need to display, I am displaying only to make user feel it is reading images. I can get rid of displaying if it solves the problem.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Saljas,

Yes. It will solve the problem. The PictureBox has lot of issues by design and while it works for most of the cases when you try to reuse it sometimes it fails. I have seen this behavior in past and the only solution was to create a copy of image and then display it in the PictureBox.

To make end user feel better you could just show a progress bar and just extract the images in background. Whether this error occurs or not, if you don't show the picture, performance of your app will increase noticeably.

Will you be interested in other ways of extracting the tiff files?

Regards,
Chinmay.

Author

Commented:
I will try your way (no image display). and get back to you.

Please provide with the extraction example.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Here is a sample I have taken from: https://code.msdn.microsoft.com/windowsdesktop/Split-multi-page-tiff-file-058050cc

        private void Split(string pstrInputFilePath, string pstrOutputPath) 
        { 
            //Get the frame dimension list from the image of the file and 
            Image tiffImage = Image.FromFile(pstrInputFilePath); 
            //get the globally unique identifier (GUID) 
            Guid objGuid = tiffImage.FrameDimensionsList[0]; 
            //create the frame dimension 
            FrameDimension dimension = new FrameDimension(objGuid); 
            //Gets the total number of frames in the .tiff file 
            int noOfPages = tiffImage.GetFrameCount(dimension); 
 
            ImageCodecInfo encodeInfo = null; 
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); 
            for (int j = 0; j < imageEncoders.Length; j++) 
            { 
                if (imageEncoders[j].MimeType == "image/tiff") 
                { 
                    encodeInfo = imageEncoders[j]; 
                    break; 
                } 
            } 
 
            // Save the tiff file in the output directory. 
            if (!Directory.Exists(pstrOutputPath)) 
                Directory.CreateDirectory(pstrOutputPath); 
 
            foreach (Guid guid in tiffImage.FrameDimensionsList) 
            { 
                for (int index = 0; index < noOfPages; index++) 
                { 
                    FrameDimension currentFrame = new FrameDimension(guid); 
                    tiffImage.SelectActiveFrame(currentFrame, index); 
                    tiffImage.Save(string.Concat(pstrOutputPath, @"\", index, ".TIF"), encodeInfo, null); 
                } 
            } 
        } 

Open in new window


There are other methods as well, some of them have a very small code footprint, I suggest you measure performance of your existing method along with this one - and if you are not satisfied, you can go after other methods.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial