Mahender
Mahender

Reputation: 5664

unable to use SaveJpeg method in windows phone 8 (NotSupportedException)

I am using below code to save a remote image in Windows Phone 8. But i keep hitting with System.NotSupportedException: Specified method is not supported. exception at SaveJpeg() method call.

I tried different combinations of method call (you can see commented line). I couldn't able to figure out what i am doing incorrectly.

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.GetAsync(imageUrl);
    await Task.Run(async () =>
    {
        if (response.IsSuccessStatusCode)
        {
            // save image locally
            Debug.WriteLine("Downloading image..." + imageName);

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!myIsolatedStorage.DirectoryExists("Images"))
                    myIsolatedStorage.CreateDirectory("Images");

                string path = imageName;
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(path);

                var buffer = await response.Content.ReadAsStreamAsync();

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    BitmapImage bitmap = new BitmapImage { CreateOptions = BitmapCreateOptions.None };

                    bitmap.SetSource(buffer);
                    WriteableBitmap wb = new WriteableBitmap(bitmap);

                    //System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);

                    wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 98);
                });
                fileStream.Close();
            }
        }
    });
}

Upvotes: 0

Views: 269

Answers (1)

Paul Annetts
Paul Annetts

Reputation: 9604

By putting the code block in BeginInvoke block you are calling the SaveJpeg on a different thread (the "UI thread") to the code which calls fileStream.Close().

In effect this means it is very likely that the call to fileStream.Close() will be called before wb.SaveJpeg.

If you move the fileStream.Close() inside the BeginInvoke block, after wb.SaveJpeg() it should work.

Upvotes: 2

Related Questions