将 BitmapSource 转换为 Image 并保留所有帧
我这里有一个方法应该生成一个 System.Drawing.Image 实例。考虑以下先决条件:
- 我将 BitmapSource 作为方法参数
- 在下面您可以找到执行此操作的代码 从 BitmapSource 到 Image 的转换。
转换:
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);
}
现在假设 BitmapSource 是从多页 Tif 文件创建的。我需要做的是使任何第 n 页在代码中可用。 BitmapSource 类不提供对此的支持,所以您知道如何从我的输入中获取除第一帧之外的任何帧吗?或者 BitmapSource 是否将整个 Tif 作为一帧读取,从而丢失了帧信息?
如果可能的话,我可以在方法签名中添加另一个参数,如下所示:
public Image ConvertBitmapSourceToImage(BitmapSource input, int frame)
{
///[..]
}
有什么想法吗?
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您已经说过的,BitmapSource 不支持多个帧。也许可以选择介入 TIFF 解码并转换每一帧的图像:
我没有测试上面的代码,对于任何缺陷,我们深表歉意。
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:
I didn't test the above code, so sorry for any flaws.