Craete tiff Image Page by Page

G

Guest

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
 
M

Marc Gravell

Last time I needed to mess with multi-page TIFs, I used ImageMan
(ActiveX at the time). Perhaps an off-the-shelf like this would help
here? e.g. ImageMan .Net. (before you ask - I'm just a satisfied
user).

Or google for a free option...

Marc
 
G

Guest

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);

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 = new System.IO.MemoryStream(atif);

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 = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);

ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);

tifImage.Save(multiStream, 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
 
G

Guest

Hi Peter,

Thanks a lot for your quick response.

Can you please tell me how can I save the created single straem as a single
tiff image.While giving the compression as "LZW" the code is working but in
the case of "CompressionCCITT4" its showing exception.

Peter can u advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer"

Thanks and Regards
Rinu G P

Peter Bromberg said:
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);

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 = new System.IO.MemoryStream(atif);

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 = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);

ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);

tifImage.Save(multiStream, 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 said:
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
 
P

Peter Duniho

Can you please tell me how can I save the created single straem as a
single tiff image.

His example writes to a MemoryStream. But there's not any reason you
can't just use a FileStream instead. Or alternatively, take the resulting
MemoryStream and write it out to a file.
While giving the compression as "LZW" the code is working but in the
case of "CompressionCCITT4" its showing exception.

What's the exception? Knowing the exception is an important clue in what
the problem is. :)

Pete
 
G

Guest

The most reliable way is to use a FileStream and simply write out the entire
byte array with the proper file extension. If you use the Image.Save method,
you run the risk of losing your encoding information due to a "not 100%"
implementation by MS.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




Rinu Gopalakrishna Pillai said:
Hi Peter,

Thanks a lot for your quick response.

Can you please tell me how can I save the created single straem as a single
tiff image.While giving the compression as "LZW" the code is working but in
the case of "CompressionCCITT4" its showing exception.

Peter can u advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer"

Thanks and Regards
Rinu G P

Peter Bromberg said:
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);

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 = new System.IO.MemoryStream(atif);

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 = new System.IO.MemoryStream();
long imgCompression = GetCompression(tifImage);

ep = new EncoderParameters(1);
ep.Param[0] = new
EncoderParameter(Encoder.Compression, imgCompression);

tifImage.Save(multiStream, 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 said:
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
 
G

Guest

can anyone advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer" .
 
G

Guest

Hi Peter,

While using "CompressionCCITT4" the exception is

"System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)"

How can I solve this problem, plz help me.

Thanks and Regards

R G P
 
P

Peter Duniho

While using "CompressionCCITT4" the exception is

"System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)"

How can I solve this problem, plz help me.

Well, sorry to say I don't have an answer. However, it sounds to me as
though the compression type is invalid. You'd think that .NET wouldn't
include it if it didn't support it, but a) there are other examples of
..NET including enumeration values for features it doesn't actually
support, and b) it may be that the compression type is supported only for
reading.

I'm making the assumption that the exact same line of code works fine when
you initialize the compression type to something else. If not, then I
suppose there's a chance the parameters are actually invalid for some
other reason.

You may need someone more familiar with the TIFF support in .NET to answer
the question for you. I don't have the first-hand experience with it to
be able to provide what I'd consider a good answer.

Pete
 
G

Guest

This might be too late but I read posts where CCITT4 compresion is related
to 1 bit pixel images (B&W). I was experiencing the same error and changed to
LZW and am not having any problems.
Glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top