views:

342

answers:

2

I'm trying to generate a multipage tiff file from an existing picture using following code: (according to this link:http://www.bobpowell.net/generating_multipage_tiffs.htm)

picture.SelectActiveFrame(FrameDimension.Page, 0);
        var image = new Bitmap(picture);
        using (var stream = new MemoryStream())
        {
            ImageCodecInfo codecInfo = null;
            foreach (var imageEncoder in ImageCodecInfo.GetImageEncoders())
            {
                if (imageEncoder.MimeType != "image/tiff") continue;
                codecInfo = imageEncoder;
                break;
            }
            var parameters = new EncoderParameters
                                 {Param= new []{new EncoderParameter(Encoder.SaveFlag, (long) EncoderValue.MultiFrame)}};
            image.Save(stream, codecInfo, parameters);

            parameters = new EncoderParameters
            {
                Param =
                    new[]
                                         {
                                             new EncoderParameter(Encoder.SaveFlag,
                                                                  (long) EncoderValue.FrameDimensionPage)
                                         }
            };
            for (var i = 1; i < picture.GetFrameCount(FrameDimension.Page); i++)
            {
                picture.SelectActiveFrame(FrameDimension.Page, i);
                var img = new Bitmap(picture);
                image.SaveAdd(img, parameters);

            }
            parameters = new EncoderParameters { Param = new[] { new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush) } };
            image.SaveAdd(parameters);
            stream.Flush();
        }

But it's not working and I don't know why .(Only the first frame is included in the image)

What I want to do is to change a particular frame of a tiff file.(adding annotations to it) I don't know if there's a simpler way to do it but what I have in mind is to create a multipage tiff from the original picture and add my own picture instead of that frame.

A: 

It seems that this process doesn't change image object but it changes the stream so I should get the memory stream buffer and build another image object:

var buffer=stream.GetBuffer();
using(var newStream=new MemoryStream(buffer))
{
var result=Image.FromStream(newStream);
}

Now result will include all frames.

Beatles1692
+1  A: 

[deleted first part after comment]

I'm working with multi-page TIFFs using LibTIFF.NET; I found many quicks in handling of TIFF using the standard libraries (memory related and also consistent crashes on 16-bit gray scale images).

What is your test image? Have you tried a many-frame tiff (preferably with a large '1' on the first frame, a '2 on the next etc; this could help you to be certain on the frame included in the file.

Another useful diagnosis may be tiffdump utility, as included in LibTiff binaries (also for windows). This will tell you exactly what frames you have.

See Using LibTiff from c# to access tiled tiff images

[Edit] If you want to understand the .NET stuff: I've found a new resource on multi-page tiffs using the standard .NET functionality (although I'll stick with LibTIFF.NET): TheCodeProject : Save images into a multi-page TIFF file... If you download it, the code snippet in Form1.cs function saveMultipage(..) is similar (but still slightly different) than your code. Especially the flushing at the end is done in a differnt way, and the file is deleted before the first frame... [/Edit]

Adriaan
Thank you Adriaan.Your answer helped me a lot cause I didn't know that there was an open source tiff library out there.I will try it immediately.About the for loop : I have used the first frame to build the image itself :picture.SelectActiveFrame(FrameDimension.Page, 0);var image = new Bitmap(picture);
Beatles1692