Few days ago at work, I have been assigned a job to write a tool to split a .tif file that contains multiple pages. So below I have some code snippets that demonstrates how to use the .NET Framework and C# to achieve this, and we don’t need to use any third party libraries.
1) First you need to create a new Console Project called SplitTIFF.
2) Add the System.Drawing assembly to your Reference folder in your project:
3) In your code add the System.Drawing and System.Drawing.Imaging namespace.
3) Add the following code in your Main method:
int activePage;
int pages;
Image image = Image.FromFile(“test.tif”);
pages = image.GetFrameCount(FrameDimension.Page);
for (int index = 0; index < pages; index++)
{
activePage = index + 1;
image.SelectActiveFrame(FrameDimension.Page, index);
image.Save(“page_” + activePage.ToString() + “.tif”);
}
Note: replace a test.tif with your own file.
You need to instantiate an Image object using the Image class static method FromFile(). Then we need to determine how many pages exist in this single .TIF file by calling the Image class GetFrameCount() method and set the parameter by passing the FrameDimension.Page enumerator.
We create a for loop to iterate through each frame or page by calling the SelectActiveFrame() method from the Image class. We then pass the FrameDimension.Page and current index as the parameter. After the active frame or page is selected we then call the Save() method to save the active frame to a single .TIF file.
After you have compiled and ran your project, you will see page_1.tif, page_2.tif… created. But this depends on how many pages exist in your .tif file. This code is very simple and straight forward but you can extend it further to accept a .tif file from the command line if you wish.
Well I hope this code snippet is useful to anyone.
Download Project Files:
SplitTIFF.zip
Thank you! Best example for TIFF splitting, other people often suggest some third party libraries requiring a much more complicated code.
This is awesome in fact.
Hi David,
your code ist great. Many Thanx.
Could you pls give an example how to split several tif files in a Folder using a for loop?
Many Thanx in advance.
Best Regards from Germany,
Sladdy
This was a great example.
Thanks a lot
Can you give a example for multiple .tiff file merged to one .tiff file
Thank you, very precise and helpful!
Nicely done David!
Just dropped to code in and ran the first time.
It’s great when you come across a simple nugget like this, thank you!
Jim
Thanks Jim!
can i split a tiff with first 5 pages.