17

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.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
ozman
  • 256
  • 1
  • 4
  • 18
  • 1
    If you are on linux, you can use [convert command](http://www.imagemagick.org/script/convert.php) to append multiple tiff images. e.g. **convert -append** _img1.tiff img2.tiff img3.tiff_ **result.jpg** – chochim Jul 26 '12 at 12:15

4 Answers4

37

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; 
        } 
} 
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
10
using System.Drawing;
using System.Drawing.Imaging;

Bitmap bm=Bitmap.FromFile("photo.tif");
bm.Save("photo.jpg",ImageFormat.Jpeg);
p.campbell
  • 98,673
  • 67
  • 256
  • 322
ketan italiya
  • 296
  • 1
  • 5
  • 23
  • 1
    Nice answer, but usually providing an explanation of what the code is doing is preferred along with the code. – MattD Apr 22 '16 at 21:56
  • For anyone wondering, you need to install the `System.Drawing.Common` nuget package in .net core 2.0 and change the type Bitmap to Image and it works. Thanks! – tedi Oct 04 '18 at 06:23
  • for me, worked with the following modification: Image bm=Image.FromFile("photo.tif") – BernieSF Aug 01 '23 at 12:13
1

We faced some problems when converting TIF files to JPEG, because TIF format supports some types of compressions that are not supported in free toolkits. I searched the internet and tried some commercial toolkits, but most of them are hard to implement with many limitations. The toolkit that drew my attention is leadtools, because it supports loading and saving many file formats (including TIF images with different compressions). We used this toolkit convert our images to JPEG format. You can find more information in the following page: http://support.leadtools.com/CS/forums/8925/ShowPost.aspx

Note that you can convert any VB.Net code to C# by using this free code converter: http://www.developerfusion.com/tools/convert/vb-to-csharp/

Roba.M
  • 11
  • 1
1
  public static class ConvertTiffToJpeg
    {
        static string base64String = null;
        public static string ImageToBase64(string tifpath)
        {
            string path = tifpath;
            using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, ImageFormat.Jpeg);
                    byte[] imageBytes = m.ToArray();
                    base64String = Convert.ToBase64String(imageBytes);
                    return base64String;
                }
            }
        }
    }

< img src="data:image/jpeg;base64, @ConvertTiffToJpeg.ImageToBase64(@"c:\sample.tif")"/>

  • 5
    What is this supposed to be? part of your answer? Then edit your post to add it. Also, how about a word or two to explain your solution? –  Dec 27 '19 at 01:01