This sample methods accepts an array of your individual loaded Tiff "pages"
passed in as MemoryStreams, and returns a single MemoryStream containing the
combined images in a single Tiff. To load a Tiff into a memoryStream, just
use a Filestream to read it into a byte array, then create a new MemoryStream
passing the byte array into the overloaded MemoryStream constructor.
public static System.IO.MemoryStream Join(System.IO.MemoryStream[]
tifsStream)
{
EncoderParameters ep = null;
System.IO.MemoryStream singleStream = new
System.IO.MemoryStream();
try
{
Image imgTif = Image.FromStream(tifsStream[0]);
long imgCompression = GetCompression(imgTif);
if (tifsStream.Length 1)
{
//
//Multi-Frame
//
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);
}
else
{
//
//Single page
//
ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);
}
//
//Save the first page
//
imgTif.Save(singleStream, CodecInfo, ep);
if (tifsStream.Length 1)
{
ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);
//
//Add the rest of pages
//
for (int i = 1; i < tifsStream.Length; i++)
{
Image pgTif = Image.FromStream(tifsStream[i]);
imgCompression = GetCompression(pgTif);
ep.Param[1] = new
EncoderParameter(Encoder.Compression, imgCompression);
imgTif.SaveAdd(pgTif, ep);
}
//
//Commit all changes
//
ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag,
(long)EncoderValue.Flush);
imgTif.SaveAdd(ep);
}
}
catch (Exception)
{
throw;
}
finally
{
if (ep != null)
ep.Dispose();
}
return singleStream;
}
/// <summary>
/// Creates a new byte array (TIF format) by combining an array
of byte arrays(TIF format).
/// </summary>
/// <param name="atif">An array of byte arrays</param>
/// <returns>An byte array.</returns>
public static byte[] Join(byte[][] atif)
{
try
{
System.IO.MemoryStream[] multiStream = new
System.IO.MemoryStream[atif.GetLength(0)];
for (int i = 0; i < multiStream.Length; i++)
multiStream[i] = new System.IO.MemoryStream(atif[i]);
System.IO.MemoryStream ms = Join(multiStream);
return ms.ToArray();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Splits an input MemoryStream (TIF format) into an array of
MemoryStream (TIF format).
/// </summary>
/// <param name="tifStream">A MemoryStream (TIF format).</param>
/// <returns>An array of MemoryStream(TIF format).</returns>
public static System.IO.MemoryStream[]
Split(System.IO.MemoryStream tifStream)
{
System.IO.MemoryStream[] multiStream ={};
EncoderParameters ep = null;
Image tifImage = null;
try
{
tifImage = Image.FromStream(tifStream);
int pgCount = tifImage.GetFrameCount(FrameDimension.Page);
multiStream = new System.IO.MemoryStream[pgCount];
for (int i = 0; i < pgCount; i++)
{
tifImage.SelectActiveFrame(FrameDimension.Page, i);
multiStream[i] = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);
ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);
tifImage.Save(multiStream[i], CodecInfo, ep);
}
}
catch (Exception)
{
throw;
}
finally
{
if (ep != null)
ep.Dispose();
if (tifImage != null)
tifImage.Dispose();
}
return multiStream;
}
--
Site:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
Short urls & more:
http://ittyurl.net
"Rinu Gopalakrishna Pillai" wrote:
Hi,
Please help me to write a dll in C# , that will read each pages of a
tiff image from a file and a memory stream object ( need two ways) and
creatre a new tiff image object.The dll should return the combined tif image
object.
Thnks in advance
Rinu G P