PngBitmapDecoder流问题

发布于 2024-11-16 00:52:58 字数 1151 浏览 4 评论 0原文

对流不太了解。为什么第一个版本可以使用文件工作,而第二个版本却不能?在“return dest;”上设置断点看起来两者都创建了完全相同的东西,但使用第二个版本的目标始终是空白图像。

    public static BitmapSource ConvertByteArrayToBitmapSource(Byte[] imageBytes, ImageFormat formatOfImage)
{
    BitmapSource dest = null;
    if (formatOfImage == ImageFormat.Png)
    {
        var streamOut = new FileStream("tmp.png", FileMode.Create);
        streamOut.Write(imageBytes, 0, imageBytes.Length);
        streamOut.Close();

        Uri myUri = new Uri("tmp.png", UriKind.RelativeOrAbsolute);
        var bdecoder2 = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        dest = bdecoder2.Frames[0];
    }

    return dest;
}

public static BitmapSource ConvertByteArrayToBitmapSource_NoWork(Byte[] imageBytes, ImageFormat formatOfImage)
{
    BitmapSource dest = null;
    using (var stream = new MemoryStream(imageBytes))
    {
        var bdecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        stream.Flush();
        dest = bdecoder.Frames[0];
        stream.Close();
    }

    return dest;
}

Don't know a whole lot about streams. Why does the first version work using a file but the second does not? Putting a breakpoint on "return dest;" it looks like both have created exactly the same thing but dest is always a blank image using the second version.

    public static BitmapSource ConvertByteArrayToBitmapSource(Byte[] imageBytes, ImageFormat formatOfImage)
{
    BitmapSource dest = null;
    if (formatOfImage == ImageFormat.Png)
    {
        var streamOut = new FileStream("tmp.png", FileMode.Create);
        streamOut.Write(imageBytes, 0, imageBytes.Length);
        streamOut.Close();

        Uri myUri = new Uri("tmp.png", UriKind.RelativeOrAbsolute);
        var bdecoder2 = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        dest = bdecoder2.Frames[0];
    }

    return dest;
}

public static BitmapSource ConvertByteArrayToBitmapSource_NoWork(Byte[] imageBytes, ImageFormat formatOfImage)
{
    BitmapSource dest = null;
    using (var stream = new MemoryStream(imageBytes))
    {
        var bdecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        stream.Flush();
        dest = bdecoder.Frames[0];
        stream.Close();
    }

    return dest;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

荒人说梦 2024-11-23 00:52:58

您必须指定 BitmapCacheOption.OnLoad,否则位图将在首次显示时加载。然而,随后该流就已经被释放了。

另外,看看这个版本支持不同的图像格式并冻结图像以获得更好的性能:

public static BitmapSource ConvertByteArrayToBitmapSource(Byte[] imageBytes)
{
    using (MemoryStream stream = new MemoryStream(imageBytes))
    {
        BitmapDecoder deconder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        BitmapFrame frame = deconder.Frames.First();

        frame.Freeze();
        return frame;
    }
}

You have to specify BitmapCacheOption.OnLoad as the bitmap will otherwise be loaded when its displayed for the first time. Then, however, the stream is already disposed.

Also, take at look at this version supporting different image formats and freezing the image for better performance:

public static BitmapSource ConvertByteArrayToBitmapSource(Byte[] imageBytes)
{
    using (MemoryStream stream = new MemoryStream(imageBytes))
    {
        BitmapDecoder deconder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        BitmapFrame frame = deconder.Frames.First();

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