从资源 dll 中获取图像作为 MemoryStream
我使用 WPF,我的程序在 DLL 资源文件中包含图像。我有一种很好的工作方式从磁盘读取图像:
Private Function GetImageFromFile(ByVal fileName As String) As BitmapImage
Dim buffer As Byte() = IO.File.ReadAllBytes(fileName)
Dim memoryStream As New IO.MemoryStream(buffer)
Dim bitmap As New BitmapImage()
bitmap.BeginInit()
bitmap.StreamSource = memoryStream
bitmap.EndInit()
bitmap.Freeze()
Return bitmap
End Function
现在,我如何从 DLL 资源中以这种 MemoryStream 方式获取图像?
基本问题:如果我简单地使用“bitmap.UriSource = 无论 uri”方式并像动画一样按顺序加载许多图像,它会建立内存。我尝试使用上面的内存流方式,它工作得很好,但后来我将图像存储在 dll 中,我不知道如何做到这一点。如果有人知道如何从托管 dll 中读取许多图像而不建立内存,请告诉我。
I use WPF and my program has images in a DLL resource file. I have this well working way to read in images from disk:
Private Function GetImageFromFile(ByVal fileName As String) As BitmapImage
Dim buffer As Byte() = IO.File.ReadAllBytes(fileName)
Dim memoryStream As New IO.MemoryStream(buffer)
Dim bitmap As New BitmapImage()
bitmap.BeginInit()
bitmap.StreamSource = memoryStream
bitmap.EndInit()
bitmap.Freeze()
Return bitmap
End Function
Now, how can I get images in this MemoryStream-way from a DLL resource?
The basic problem: If I use simply the "bitmap.UriSource = whatever uri" way and load many images in sequence like an animation it builds up the memory. I tried with the above memorystream way and it worked perfectly fine, but then I store my images in a dll and I don't know how to do this trick. If anybody knows how to read many images from a managed dll without building up the memory pls, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了我的问题的答案。我把它放在这里,供其他可能需要它的人使用。有更多方法可以从资源 dll 文件加载图像。初始化 BitmapImage 并设置 bi.UriSource=uriPath (其中路径如下所示)的最简单方法,但是当您按顺序加载图像(例如作为动画)时,它似乎会占用大量内存。然后,您可以使用 StreamResourceInfo,如下所示,只需输入 bi.StreamSource = sri.Stream 即可。这也有效,但从记忆角度来看,它有相同的结果。因此,在实践中,我发现以下方法是按顺序加载数百个图像的最快且最节省内存的方法:
其中 uriPath 类似于:“pack://application:,,,/YourDLL;Component/YourImageFile.jpg”
I found the answer to my question. I put it here for others who may need it. There are more ways to load images from a resource dll file. The easiest way to initialize the BitmapImage and set bi.UriSource=uriPath (where the path looks like I show below) but when you load images in a sequence, as an animation for example, it seems to take a lot of memory. Then you can use a StreamResourceInfo as shown below and just put like bi.StreamSource = sri.Stream. That works, too, but memorywise it has same results. So in practice I found the following way the fastest and most memory efficient way to load hundreds of images in a sequence:
Where the uriPath is something like: "pack://application:,,,/YourDLL;Component/YourImageFile.jpg"