Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Craete tiff Image Page by Page

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rinu Gopalakrishna Pillai - 10 Apr 2007 14:54 GMT
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
Marc Gravell - 10 Apr 2007 15:09 GMT
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
Peter Bromberg [C# MVP] - 10 Apr 2007 16:34 GMT
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;
           }

Signature

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

> Hi,
>
[quoted text clipped - 6 lines]
>
> Rinu G P
Rinu Gopalakrishna Pillai - 10 Apr 2007 20:38 GMT
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

> This sample methods accepts an array of your individual loaded Tiff "pages"
> passed in as MemoryStreams, and returns a single MemoryStream containing the
[quoted text clipped - 170 lines]
> >
> > Rinu G P
Peter Duniho - 10 Apr 2007 22:06 GMT
> 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
Rinu Gopalakrishna Pillai - 11 Apr 2007 15:26 GMT
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

> > Can you please tell me how can I save the created single straem as a  
> > single tiff image.
[quoted text clipped - 10 lines]
>
> Pete
Peter Duniho - 11 Apr 2007 17:38 GMT
>     While using "CompressionCCITT4" the exception is
>
[quoted text clipped - 3 lines]
>
> 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
Glen M - 11 May 2007 14:22 GMT
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

> >     While using "CompressionCCITT4" the exception is
> >
[quoted text clipped - 21 lines]
>
> Pete
Peter Bromberg [C# MVP] - 10 Apr 2007 22:14 GMT
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

Signature

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

> Hi Peter,
>
[quoted text clipped - 184 lines]
> > >
> > > Rinu G P
Rinu Gopalakrishna Pillai - 11 Apr 2007 14:02 GMT
can anyone advise me , is there any better and faster method for returning
the tiff image from a dll other than "Memory buffer" .
Jay Riggs - 11 Apr 2007 16:51 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.