开发者

Generating a multipage TIFF is not working

I'm trying to generate a multipage TIFF file from an existing picture using code by Bob Powell:

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 (only the first frame is included in the image) and I don't know why.

What I want to do is to change a particular frame of a TIFF file (add 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.


[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]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜