Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

appending pages to tiff files

56 views
Skip to first unread message

Al

unread,
Jul 2, 2004, 1:10:10 PM7/2/04
to
Is there a way to append "pages" to existing tiff files? I've tried using
File.Append method from System.IO, and the resulting file is indeed twice as
big, but when displayed only the pages from the original file can be
accessed.

Any help, pointers, etc would be greatly appreciated.

TIA!


Bob Powell [MVP]

unread,
Jul 4, 2004, 8:00:30 AM7/4/04
to
Appending pages to an existing multi-page TIFF is difficult in many
respects.

First of all this requires the extraction of the existing images and the
breaking down of the original file into an array of individual bitmaps. This
*should* be possible using the SelectActiveFrame method and then drawing the
images to an in-memory bitmap.

I had intended to do an article on this for the GDI+ FAQ because it's
cropped up several times in the past month but to my horror I fint that
SelectActiveFrame for FrameDimension.Page seems to throw an exception for
anything other than the first frame. I am determining at the moment whether
this is a real bug or a result of my late-night programming.

Either way..I'll inform the group of the findings.

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Al" <codef...@cox.net> wrote in message
news:ug8XoeFY...@TK2MSFTNGP11.phx.gbl...

Bob Powell [MVP]

unread,
Jul 4, 2004, 2:51:41 PM7/4/04
to
Hi, The code only works for creating a new multipage tiff. Appending is a different process and cannot be accomplished by pasting another file onto the end of an existing one.

--
Bob Powell [MVP]
Visual C#, System.Drawing
 
The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm
 
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 
 
 
 
 
 
 
"Al Knowles" <codef...@cox.net> wrote in message news:%23PNRNcc...@TK2MSFTNGP10.phx.gbl...
Hi, Bob.  I'm working through your article at this link

http://www.bobpowell.net/generating_multipage_tiffs.htm

So far, the most difficult part has been translating the C sharp code
into VB.NET.  I am almost to the point where I can test the code.

I'm only attempting to append uploaded single-page tiff files into
existing multi-page tiff files.  Should the code you posted work for
this?

Thanks.

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Bob Powell [MVP]

unread,
Jul 4, 2004, 2:52:58 PM7/4/04
to
If you look real close you'll see that the code is in C# *and* VB.. ;-)

--
Bob Powell [MVP]
Visual C#, System.Drawing
 
The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm
 
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 
 
 
 
 
 
 
"Al Knowles" <codef...@cox.net> wrote in message news:%23PNRNcc...@TK2MSFTNGP10.phx.gbl...

Al

unread,
Jul 4, 2004, 4:41:31 PM7/4/04
to
This guy says he got it to work....so I guess I'll try to translate his code
to VB.NET instead. (Nice to have it in a function already!)

----------------------------------------------------------------------------
------------------------------------------
Here is my code. You will have to adapt it to your need. Originally, it
comes from the Bob Powell tutorial I mentioned above.

Basically, all you need to do is pass in an array of one page images and a
destination file name. I opted for one page images to normalize the
workflow. If all of your images aren't already one page, just right another
function to separate the pages.

Sample usage:

ToMultiPageImage( new Bitmap[]{ Bitmap.FromFile( "img1.tif" ),
Bitmap.FromFile( "img2.tif" ) }, "multi.tif" );
---
/// <summary>
/// Gets the ImageCodecInfo for the mime type named <c>mimeType</c>.
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public static ImageCodecInfo GetCodec( string mimeType )
{
foreach( ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders() )
if( ice.MimeType.Equals( mimeType ) )
return ice;

throw new Exception( mimeType + " mime type not found in
ImageCodecInfo" );
}

/// <summary>
/// Saves the images in pages to a single file named <c>fileName</c>.
/// </summary>
/// <param name="pages"></param>
/// <param name="fileName"></param>
/// <param name="p">Additional encoder parameters.</param>
/// <returns></returns>
public static Bitmap ToMultiPageImage( Image[] pages, string fileName,
params EncoderParameter[] p )
{
Bitmap image = new Bitmap( pages[0] );
FileStream stream = new FileStream( fileName, FileMode.OpenOrCreate );

// Get an encoder for saving with
Encoder enc = Encoder.SaveFlag;
//Obtain the TIFF codec info.
ImageCodecInfo info = GetCodec( "image/tiff" );
//Create a parameter list. This needs 1 parameter in it.
EncoderParameters ep = new EncoderParameters( p.Length + 1 );
//Place the MultiFrame encoder value in the parameter list
ep.Param[0] = new EncoderParameter( enc, (long)
EncoderValue.MultiFrame );

// ************* Add additional parameters **************
int index = 1;
foreach( EncoderParameter param in p )
ep.Param[ index++ ] = param;
// ******************************************************

//Save the first frame using the encoder and parameters
image.Save( stream, info, ep );
//Change the encoder value in the list to FrameDimensionPage
ep.Param[0] = new EncoderParameter( enc, (long)
EncoderValue.FrameDimensionPage );
//Use first of the master frame's overloaded SaveAdd methods to add
subsequent images. Repeat this step for as many images as you want to add.
for( int i = 1; i < pages.Length; i++ )
image.SaveAdd( pages[ i ], ep );
//Use the second of the master frames overloaded SaveAdd methods to flush,
save and close the image.
ep.Param[0] = new EncoderParameter( enc, (long) EncoderValue.Flush );
image.SaveAdd(ep);

stream.Close();
return image;
}
Post edited by flipdoubt on 06/21/2004 09:23:32


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ONGDlffY...@TK2MSFTNGP09.phx.gbl...

Al

unread,
Jul 4, 2004, 4:42:43 PM7/4/04
to
Here is the link in case anyone wants it:

http://gotdotnet.com/Community/MessageBoard/Thread.aspx?id=225137


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ONGDlffY...@TK2MSFTNGP09.phx.gbl...

Bob Powell [MVP]

unread,
Jul 4, 2004, 7:44:54 PM7/4/04
to
Looking at what is in that thread I see that none of the people there are
trying to append an image to a multi-page tiff file.

My code takes individual images, tiff, bmp, jpeg whatever and amalgamates
them into a single multi-page TIFF file.

You *can* add a tiff to a tiff if all you want is a 2 page tiff. However, as
far as I know if you already have a 2 page TIFF and want to make it a 3
pager, there seem to be some severe problems that I am currently
investigating with the help of some people from Microsoft.

That's not to say you shouldn't try though :-) If you get a successful
solution pleas mail me and let me know. I'll write it up in the FAQ and
credit you (or whoever solves it)

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm


"Al" <codef...@cox.net> wrote in message
news:%23NiGgeg...@TK2MSFTNGP10.phx.gbl...

Neo The One

unread,
Aug 20, 2004, 9:03:03 AM8/20/04
to
Hey, I'm not sure if anyone is still interested in this topic, but I have a
code piece that works fine with blank & white TIFF images.

The code piece below took me 8 hours! :-( During this 8 hours, I met the
same errors that I saw others have posted in this forum. Specifically they
are:

1. Image.FromFile throws 'Out of memory' excetion when loading a TIFF image
file.

2. Image.SelectActiveFrame throws generic GDI+ exception.

Can you guess what I found out about these two errors? To be modest, I found
only one cause for the errors. That is, if you have page in the TIFF file
that is saved using JPEG compression, then you will be blown up. :-p

OK, there is the code (it may be a littley messy, but you see I am just
doing tests :-)):

private void button1_Click(object sender, System.EventArgs e)
{
Image dog;
Bitmap test;
Image tmp;
Encoder enc;
string s;
int n;
EncoderParameters eps;
ImageCodecInfo info;
MemoryStream ms;

enc = Encoder.SaveFlag;
eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);

info = null;
foreach(ImageCodecInfo item in ImageCodecInfo.GetImageEncoders())
{
if("image/tiff" == item.MimeType)
{
info = item;
break;
}
}

OpenFileDialog dlg;

dlg = new OpenFileDialog();
if(DialogResult.OK != dlg.ShowDialog(this)) return;

s = dlg.FileName;
dog = Image.FromFile(s);
n = dog.GetFrameCount(FrameDimension.Page);

test = new Bitmap(dog);
test.RotateFlip(RotateFlipType.Rotate180FlipX);
test.Save(Path.Combine(Path.GetTempPath(), "test.tiff"), info, eps);

ms = new MemoryStream();

eps.Param[0] = new EncoderParameter(enc,
(long)EncoderValue.FrameDimensionPage);
for(int i = 1; i < n; i++)
{
dog.SelectActiveFrame(FrameDimension.Page, i);
dog.Save(ms, ImageFormat.Bmp);

tmp = Image.FromStream(ms);
tmp.RotateFlip(RotateFlipType.Rotate180FlipX);

test.SaveAdd(tmp, eps);

ms.SetLength(0);
}
dog.Dispose();
test.Dispose();

MessageBox.Show(this, "Done!", "Done");

yerf...@gmail.com

unread,
Mar 21, 2013, 7:54:41 AM3/21/13
to
??? is it a bug??
0 new messages