图像标头无法识别。 (WindowsES 8应用程序中的HRESULT异常:0x88982F61)

问题描述 投票:4回答:3

这是我的代码......

    public async Task SetLargeImageAsync(byte[] imageBytes,
        bool storeBytesInObject = false)
    {

        var tcs = new TaskCompletionSource<string>();

        SmartDispatcher.BeginInvoke(() =>
        {
            using (MemoryStream ms = new MemoryStream(imageBytes))
            {

                if (storeBytesInObject)
                    this.LargeImageBytes = imageBytes;

                BitmapImage image = new BitmapImage();

                image.SetSource(ms);

                this.LargeImage = image;

                tcs.SetResult(string.Empty);
            }
        });

        await tcs.Task;
    }

我正在将字节发送到流中。这很好用;它正在显示一个图像。

但有时我会得到以下异常:

图像标头无法识别。 (来自HRESULT的异常:0x88982F61)MS.Internal.XcpImports.CheckHResult(UInt32 hr)位于System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)的MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource,CValue&byteStream) System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)中的System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)

问题是什么?不同类型的图像有什么问题吗?

我找到了一个地方,我们应该使用以下代码来寻找起始位置:

ms.Seek(0, SeekOrigin.Begin)

这是真的吗?解决方案是什么?

image windows-phone-8
3个回答
2
投票

您传入的图像无效 - 要么已损坏,要么以WP无法解码的格式存储。有关支持格式的完整列表,请访问:http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx#BKMK_ImageSupport


2
投票

在开始操作之前,请确保imageBytes.Position = 0。


0
投票

正如MarcosVasconcelos的评论中所提到的,我的解决方案是在写入流之前和设置BitmapImage Stream源之前将MemoryStream中的位置设置为开头。

例:

public static BitmapImage CreateImage(byte[] src)
        {
            var img = new BitmapImage();
            var strm = new System.IO.MemoryStream();           

            strm.Write(src, 0, src.Length);

            // set the position of stream to 0 after writing to it
            strm.Seek(0, System.IO.SeekOrigin.Begin);

            img.BeginInit();
            img.StreamSource = strm;
            img.EndInit();
            return img;
        }
© www.soinside.com 2019 - 2024. All rights reserved.