472,145 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Craete tiff Image Page by Page

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
Apr 10 '07 #1
10 10431
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
Apr 10 '07 #2
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
Apr 10 '07 #3
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 [C# MVP]" wrote:
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
Apr 10 '07 #4
On Tue, 10 Apr 2007 12:38:02 -0700, Rinu Gopalakrishna Pillai
<Ri*********************@discussions.microsoft.com wrote:
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
Apr 10 '07 #5
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" wrote:
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 [C# MVP]" wrote:
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
Apr 10 '07 #6

can anyone advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer" .
Apr 11 '07 #7
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

"Peter Duniho" wrote:
On Tue, 10 Apr 2007 12:38:02 -0700, Rinu Gopalakrishna Pillai
<Ri*********************@discussions.microsoft.com wrote:
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
Apr 11 '07 #8
On Apr 11, 6:02 am, Rinu Gopalakrishna Pillai
<RinuGopalakrishnaPil...@discussions.microsoft.com wrote:
can anyone advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer" .
Hi Rinu,

I'm not entirely sure what you're looking for. Are you trying to
create a dll that combines single page tiffs into a single multipage
tiff?

If so, perhaps the following will help you get started:
http://www.bobpowell.net/generating_multipage_tiffs.htm
http://www.bobpowell.net/addframes.htm
HTH
-Jay

Apr 11 '07 #9
On Wed, 11 Apr 2007 07:26:05 -0700, Rinu Gopalakrishna Pillai
<Ri*********************@discussions.microsoft.com wrote:
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
Apr 11 '07 #10
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

"Peter Duniho" wrote:
On Wed, 11 Apr 2007 07:26:05 -0700, Rinu Gopalakrishna Pillai
<Ri*********************@discussions.microsoft.com wrote:
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
May 11 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nicolas Guilhot | last post: by
1 post views Thread by Prasad More | last post: by
2 posts views Thread by Al Reid | last post: by
6 posts views Thread by qysbc | last post: by
1 post views Thread by Stedak | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.