473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 10656
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
1
by: Bob | last post by:
I have a query whose results contain the full path (i.e. c:\Images\xxx.tiff) to MUTI-PAGE (All two page) TIFF documents. I need to figure out how to BATCH print the tiff's using the path from...
1
by: Prasad More | last post by:
Hello, I am trying to write a text on Multi-page TIFF image using C# and .NET GDI+. I have written following code to do this. When I execute this code I get "Invalid Parameter User. at...
3
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the...
2
by: Al Reid | last post by:
Is it possible to display an image that is stored on the server as a TIFF image, on an ASP.Net page without the use of an add-in viewer? If so, could someone tell me how to do it? TIA -- Al...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
6
by: qysbc | last post by:
I have a web page and there is a link to open a TIFF file. The way I do it is to have the server code open a binary stream, set the content type to "image/tiff" and call Response.BinaryWrite. On...
3
by: CD | last post by:
An application is logging faxes sent in SQL2000 image column type. I have found code on the net but what it is doing is prompting to save to local which is fine for single page image. Not good...
1
by: Stedak | last post by:
I have the following class I use to save Tiff's. The problem I have with it is that the final size of the images are very large. If we scan directly to a file the final tiff may be 600-900 kb.s but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.