Frage

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!

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top