Search code examples
c#tiffjpeg

Convert TIFF to JPG format


I have a TIFF file with two pages. When I convert the file to JPG format I lose the second page. Is there any way to put two images from a TIFF file into one JPG file?

Because TIFF files are too big, I have to decrease their sizes. Is there any way to decrease TIFF size programmatically? That could also help solve my problem.


Solution

  • Since a TIFF can contain multiple frames but JPG can't, you need to convert each single frame into a JPG.

    Taken from Windows Dev Center Samples:

    public static string[] ConvertTiffToJpeg(string fileName) 
    { 
            using (Image imageFile = Image.FromFile(fileName)) 
            { 
                FrameDimension frameDimensions = new FrameDimension( 
                    imageFile.FrameDimensionsList[0]); 
    
                // Gets the number of pages from the tiff image (if multipage) 
                int frameNum = imageFile.GetFrameCount(frameDimensions); 
                string[] jpegPaths = new string[frameNum]; 
    
                for (int frame = 0; frame < frameNum; frame++) 
                { 
                    // Selects one frame at a time and save as jpeg. 
                    imageFile.SelectActiveFrame(frameDimensions, frame); 
                    using (Bitmap bmp = new Bitmap(imageFile)) 
                    { 
                        jpegPaths[frame] = String.Format("{0}\\{1}{2}.jpg",  
                            Path.GetDirectoryName(fileName), 
                            Path.GetFileNameWithoutExtension(fileName),  
                            frame); 
                        bmp.Save(jpegPaths[frame], ImageFormat.Jpeg); 
                    } 
                } 
    
                return jpegPaths; 
            } 
    }