Here is my code...
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;
}
I am sending bytes into stream. This works fine; it is showing an image.
But I am getting the following exception sometimes:
The image header is unrecognized. (Exception from HRESULT: 0x88982F61) at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream) at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource) at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource) at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
What is the problem? Is there any problem with different types of images?
I found somewhere that we should use following code for seeking beginning position:
ms.Seek(0, SeekOrigin.Begin)
Is it true? What is the solution for that?
Make sure that imageBytes.Position = 0 before beginning your operations.
You're passing in an invalid image - either it's corrupted or stored in a format that WP can't decode natively. The full list of supported formats can be found at: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx#BKMK_ImageSupport
As also mentioned in the comment by MarcosVasconcelos, the solution for me was to set the position within the MemoryStream to the beginning after writing to the stream and before setting the BitmapImage Stream source.
Example:
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;
}
User contributions licensed under CC BY-SA 3.0