将 ImageSource 转换为 BitmapImage - WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/920517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert ImageSource to BitmapImage - WPF
提问by Alexis Mathieu
I'm using a class library that generates a large ImageSource, > 3000x3750 pixels. I would like to convert this ImageSource to BitmapImage so that I can take advantage of DecodePixelWidth or DecodePixelHeight instead of resizing it everytime this image is generated.
我正在使用一个类库,它生成一个大的 ImageSource,> 3000x3750 像素。我想将此 ImageSource 转换为 BitmapImage 以便我可以利用 DecodePixelWidth 或 DecodePixelHeight 而不是每次生成此图像时调整它的大小。
I need to display this image for the user first, and most of the users have a screen resolution of 1024x768, I'm binding this ImageSource to an Image control for this, but it can be noticed how "heavy" it is.
我需要先为用户显示此图像,并且大多数用户的屏幕分辨率为 1024x768,为此我将此 ImageSource 绑定到一个 Image 控件,但可以注意到它是多么“重”。
How can I do this? Or what is the best solution for this case?
我怎样才能做到这一点?或者这种情况的最佳解决方案是什么?
thanks!
谢谢!
回答by Alexis Mathieu
I know it's a old post, but try doing:
我知道这是一个旧帖子,但请尝试执行以下操作:
myBitmapImage = myImageSource as BitmapImage;
That works well.
这很好用。
回答by Noldorin
The BitmapImage
type inherits BitmapSource
and ultimately ImageSource
(both of which are abstract classes). You need to check what the actual type of your object is, i.e. check object.GetType().Name
. If you're in luck, it may actually be returning a BitmapSource
object and you will simply need to cast it to that type before being able to use it as such.
的BitmapImage
类型继承BitmapSource
和最终ImageSource
(两者是抽象类)。您需要检查对象的实际类型是什么,即 check object.GetType().Name
。如果幸运的话,它实际上可能会返回一个BitmapSource
对象,您只需将其强制转换为该类型,然后才能使用它。