In Focus
MOBILE: HTML 5 vs. Native Apps
Istanbul Chapter May 2013 2nd Seminar Announced : Learn WinJS, Windows Store, MVC
ANNOUNCEMENT: April 2013 Month Winners
Email :
Password :
Remember me?
Forgot password
Contribute
An Article
A Blog
A News
A Video
A Link
An Interview Question
Ask a Question
TECHNOLOGIES
.NET 4.5
Current Affairs
JQuery
SharePoint 2010
Web Development
.NET Assemblies
Databases & DBA
Learn .NET
SharePoint 2013
Web Services in C#
ADO.NET in C#
Design & Architecture
LightSwitch 2012
Silverlight with C#
Windows 8
AJAX in C#
Exception Handling C#
LINQ
Smart Devices
Windows Azure
Android Programming
Graphics Design
Mobile & Embedded
SQL
Windows Controls C#
Articles C#
Hardware
Office 2013
SQL Server 2012
Windows Forms C#
ASP.NET Controls in C#
How do I
OOP/OOD
Testing
Windows Phone 8
ASP.NET MVC with C#
HTML 5
Operating Systems
TypeScript
Windows Server 2012
ASP.NET Programming
Internet & Web
PHP
Visual C#
Windows Store Apps
C# Language
iPhone/iPad
Products
Visual Studio .NET
Workflow Foundation in C#
C# Tutorials
Java
Reports using C#
Visual Studio 2010
WPF
C, C++, MFC
Java and .NET
Robotics & Hardware
Visual Studio 2012
XAML
Career Advice
JavaScript, CSS
Security in .NET
WCF with C#
XML
Chapters
Request a new Category
|
View All
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
LINKS
NEWS
CHAPTERS
CAREER ADVICE
After 3rd C# Corner ISTANBUL Chapter Seminar
HTML 5 vs Native Apps
GridView New Feature in ASP.NET 4.0
Secure Your Android Application from Hackers
Why to fear Object Oriented Programming approach
Check upcoming B'day in Asp.net
Happy Birthday Destin Joy
Access Master Page Control In Child Page
Popup Example in WPF
Example of Expander in WPF
Blog
How to SAVE, SPLIT, MERGE, and VIEW multipage TIFF image
Posted by
Mark Fabro
in
Blogs
|
.NET 4.5
on
Feb 04, 2013
You can use this sample code in Document Scanning System and Document Management System.
Tweet
872
0
1
You can use this class to save the output of merged and splitted file.
To view the image per page on PictureBox or any picture control tool, get its frame example
image.SelectActiveFrame(frameDim, Pages[i];
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace DocImageUtility
{
public class TIFManager
{
private string TIFF_CODEC = "image/tiff";
private long ENCODING_SCHEME = (long)EncoderValue.CompressionCCITT4;
public string SelectedPages { get; set; }
public int NumberOfPages { get; set; }
public string FilePath { get; set; }
public bool newfile = true;
public bool newdocument = true;
Image Document;
EncoderParameters encoderParams;
public int SelectedPageCount { get { return Pages.Length; } }
//First method to call in order to load the file and get its contents and pages
public void Load(string path)
{
this.FilePath = path;
//Get the frame dimension list from the image of the file and
Image tiffImage = Image.FromFile(path);
//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
NumberOfPages = tiffImage.GetFrameCount(dimension);
tiffImage.Dispose();
}
//Split Method
public void Split(string splittedFileName)
{
//Get its file information
ImageCodecInfo codecInfo = GetCodecInfo(TIFF_CODEC);
EncoderParameters encoderParams = new EncoderParameters(2);
encoderParams.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
encoderParams.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ENCODING_SCHEME);
//Load the Document
FileStream fs = new FileStream(this.FilePath, FileMode.OpenOrCreate);
Image image = Image.FromStream(fs);
//Get file frame/pages
FrameDimension frameDim = new FrameDimension(image.FrameDimensionsList[0]);
//Check if selected pages is null or 0 value
if (SelectedPages != null)
{
// Delete / Overwrite existing file if updating Splitted Image File
var file = new FileInfo(splittedFileName);
if (file.Exists) file.Delete();
//for each frame/pages create the new document
for (int i = 0; i < Pages.Length; i++)
{
//check whether selected pages is not greater than the file pages
if (Pages.Length >= (i + 1))
{
//Selected image frame
image.SelectActiveFrame(frameDim, Pages[i]);
//check whether file is new document
if (newfile == true)
{
image.Save(splittedFileName, codecInfo, encoderParams);
newfile = false;
}
else
//append the document depending on the selected frame from the original image
{
encoderParams.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);
image.SaveAdd(image, encoderParams);
}
}
}
fs.Close();
}
}
//Merge Method
public void Merge(string MergeFileName, string source)
{
//Set its Image Format as TIFF
ImageCodecInfo codecInfo = GetCodecInfo(TIFF_CODEC);
//Load the Document
FileStream fs = new FileStream(source, FileMode.OpenOrCreate);
Image image = Image.FromStream(fs);
//Get file frame/pages
FrameDimension frameDim = new FrameDimension(image.FrameDimensionsList[0]);
if (newdocument == true)
{
//Set its Image Type
encoderParams = new EncoderParameters(2);
encoderParams.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
encoderParams.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ENCODING_SCHEME);
//Check if selected pages is null or 0 value
if (SelectedPages != null)
{
//for each frame/pages create the new document
for (int i = 0; i < image.GetFrameCount(frameDim); i++)
{
//check whether selected pages is not greater than the file pages
if (Pages.Length >= (i + 1))
{
//Selected image frame
image.SelectActiveFrame(frameDim, Pages[i]);
//check whether file is new document
if (newfile == true)
{
//create new filename
Document = image;
//save
Document.Save(MergeFileName, codecInfo, encoderParams);
encoderParams.Param[0] =
new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);
newfile = false;
newdocument = false;
}
else
{
//append the document depending on the selected frame from the original image
Document.SaveAdd(image, encoderParams);
}
}
}
fs.Close();
}
}
else
{
//Check if selected pages is null or 0 value
if (SelectedPages != null)
{
//for each frame/pages create the new document
for (int i = 0; i < image.GetFrameCount(frameDim); i++)
{
//check whether selected pages is not greater than the file pages
if (Pages.Length >= (i + 1))
{
//Selected image frame
image.SelectActiveFrame(frameDim, Pages[i]);
//check whether file is new document
//append the document depending on the selected frame from the original image
Document.SaveAdd(image, encoderParams);
}
}
fs.Close();
}
}
}
//Check whether selected pages is valid
public int[] Pages
{
get
{
ArrayList ps = new ArrayList();
string[] ss = SelectedPages.Split(new char[] { ',', ' ', ';' });
foreach (string s in ss)
if (Regex.IsMatch(s, @"\d+-\d+"))
{
int start = int.Parse(s.Split(new char[] { '-' })[0]);
int end = int.Parse(s.Split(new char[] { '-' })[1]);
if (start > end)
return new int[] { 0 };
while (start <= end)
{
ps.Add(start - 1);
start++;
}
}
else
{
int i;
int.TryParse(s, out i);
if (i > 0)
ps.Add(int.Parse(s) - 1);
}
return ps.ToArray(typeof(int)) as int[];
}
}
private ImageCodecInfo GetCodecInfo(string codec)
{
ImageCodecInfo codecInfo = null;
foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
{
if (info.MimeType == codec)
{
codecInfo = info;
break;
}
}
return codecInfo;
}
}
}
This Feature is Sponsored By
DynamicPDF Merger is a developers dream for interacting with any existing PDF documents. Merge, append, split, form fill, flatten stamp and so much more.
How to add favicon to the web-si..
VS 2010 Keyboard Shortcut posters
post comment
COMMENT USING
PREMIUM SPONSORS
DynamicPDF Developer Components
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
More Blogs from this Blogger
How to SAVE, SPLIT, MERGE, and VIEW multipage TIFF image
View All
SPONSORED BY
DynamicPDF ReportWriter Suite
PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Follow @twitterapi
Latest Blogs
HTML 5 vs Native Apps
Visual C# Introduction
Software Engineering Paradigm
SQL Server Management Studio Keyboard Shortcuts
Operation taking longer than expected - Visual Studio 2012
Aligning a button to right bottom corner of the screen(Model Window) in Lightswitch
Why to fear Object Oriented Programming approach
Happy Birthday Destin Joy
Access Master Page Control In Child Page
Find Dependent Object In SQL Server
View All
Poll
Result
All Polls
Favorite Computer Manufacturer
Who is your favorite Computer manufacturer?
Lenovo
HP
Dell
Toshiba
Acer
Sony
Who is your favorite Computer manufacturer?
Options
Votes
%
Lenovo
21
14.38
HP
6
4.11
Dell
17
11.64
Toshiba
54
36.99
Acer
43
29.45
Sony
5
3.42
Total
146
100%