从资源 dll 中获取图像作为 MemoryStream

发布于 2024-12-11 09:41:32 字数 644 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2024-12-18 09:41:32

我找到了我的问题的答案。我把它放在这里,供其他可能需要它的人使用。有更多方法可以从资源 dll 文件加载图像。初始化 BitmapImage 并设置 bi.UriSource=uriPath (其中路径如下所示)的最简单方法,但是当您按顺序加载图像(例如作为动画)时,它似乎会占用大量内存。然后,您可以使用 StreamResourceInfo,如下所示,只需输入 bi.StreamSource = sri.Stream 即可。这也有效,但从记忆角度来看,它有相同的结果。因此,在实践中,我发现以下方法是按顺序加载数百个图像的最快且最节省内存的方法:

Public Function GetImageFromDLL(ByVal uriPath As String) As BitmapImage
    Dim sri As Windows.Resources.StreamResourceInfo = Application.GetResourceStream(New Uri(uriPath, UriKind.Absolute))
    Dim binReader As New System.IO.BinaryReader(sri.Stream)
    Dim buffer As Byte() = binReader.ReadBytes(sri.Stream.Length)
    Dim memoryStream As New IO.MemoryStream(buffer)

    Dim bi As New BitmapImage()
    bi.BeginInit()
    bi.CacheOption = BitmapCacheOption.Default
    bi.CreateOptions = BitmapCreateOptions.None
    bi.StreamSource = memoryStream
    bi.EndInit()
    bi.Freeze()

    Return bi
End Function

其中 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:

Public Function GetImageFromDLL(ByVal uriPath As String) As BitmapImage
    Dim sri As Windows.Resources.StreamResourceInfo = Application.GetResourceStream(New Uri(uriPath, UriKind.Absolute))
    Dim binReader As New System.IO.BinaryReader(sri.Stream)
    Dim buffer As Byte() = binReader.ReadBytes(sri.Stream.Length)
    Dim memoryStream As New IO.MemoryStream(buffer)

    Dim bi As New BitmapImage()
    bi.BeginInit()
    bi.CacheOption = BitmapCacheOption.Default
    bi.CreateOptions = BitmapCreateOptions.None
    bi.StreamSource = memoryStream
    bi.EndInit()
    bi.Freeze()

    Return bi
End Function

Where the uriPath is something like: "pack://application:,,,/YourDLL;Component/YourImageFile.jpg"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文