1

I have a method here that is supposed to produce a System.Drawing.Image instance. Consider the following prerequesites:

  • I get a BitmapSource as a method parameter
  • Below you find the code that does the transformation from BitmapSource to Image.

Conversion:

public Image ConvertBitmapSourceToImage(BitmapSource input)
{
    MemoryStream transportStream = new MemoryStream();
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(input));
    enc.Save(transportStream);
    transportStream.Seek(0, SeekOrigin.Begin);
    return Image.FromStream(transportStream);
}

Now imagine the BitmapSource has been created from a Multipage Tif file. What I need to do is make any nth page available in code. The BitmapSource class offers no support for this, so do you know a way how the get any one but the first frame out of my input? Or does BitmapSource read in the whole Tif as one frame, losing the framing information?

If it were possible, I could add another parameter to my method signature, like so:

public Image ConvertBitmapSourceToImage(BitmapSource input, int frame)
{
   ///[..]
}

Any Ideas?

Thanks in advance!

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60
  • It's not clear to me how your multiple images should be displayed or even 'navigated'. Should they lie side by side or maybe on top of each other with a mouse click stepping through them. Please provide a bit more information about how such a multi-image control would be used. – Clemens Jan 26 '12 at 12:32
  • Sorry, that was probably not well formulated. I intend to display the current Page only, Navigation just increases or decreases the PageSelection. So, when navigation moves forward, I want to display the second frame of the Image instead of the first. The image class supports multiframes, but all I get as an input is a BitmapSource (generated from a Multipage Tiff file). – Sebastian Edelmeier Jan 27 '12 at 09:20
  • 1
    [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.controls.image.aspx) says `When displaying a multiframe image, only the first frame is displayed. The animation of multiframe images is not supported by the Image control`, so you might be better off with creating one Image per frame (and i guess put them on separate pages or so). – Clemens Jan 27 '12 at 09:40
  • You refer to the Image control. I was talking about System.Drawing.Image, which has Support for multiframes. I will update the question to make this understood. – Sebastian Edelmeier Jan 27 '12 at 10:20
  • Right, i was confusing that. Could have deduced it from Image.FromStream. You might add the system.drawing tag to your question. – Clemens Jan 27 '12 at 10:40

1 Answers1

0

As you've said already, a BitmapSource does not support multiple frames. Perhaps it would be an option to step in at the point where the TIFF is decoded and convert an Image from every frame:

TiffBitmapDecoder decoder = new TiffBitmapDecoder(...) // use stream or uri here
System.Drawing.Image[] images = new System.Drawing.Image[decoder.Frames.Count];

for (int i = 0; i < decoder.Frames.Count; i++)
{
    // use your converter function here
    images[i] = ConvertBitmapSourceToImage(decoder.Frames[i]));
}

I didn't test the above code, so sorry for any flaws.

Clemens
  • 123,504
  • 12
  • 155
  • 268