Saving a WriteableBitmap

2019-04-13 02:28发布

"mai" is the grid name that contains the image, text and small image. I was following a blog post about being able add to your image by making it a WriteableBitmap (with a UIelment).

    try
    {
        WriteableBitmap wbm = new WriteableBitmap(mai, null);

        MediaLibrary ml = new MediaLibrary();
        Stream stream = new MemoryStream();

        wbm.SaveJpeg(stream, wbm.PixelWidth, wbm.PixelHeight, 0, 100);
        ml.SavePicture("mai.jpg", stream);
        MessageBox.Show("Picture Saved...");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }

When I run this in debug mode on the emulator I get an Unexpected Error message. I've also deployed this app to my phone (and disconnected it from the computer) and received the same error.

Basically I'm trying to save a cropped image picked from the Camera Roll with some text overlayed on top of it. It like to save this "new" image into the Camera Roll.

Update:

I also did this with the same result:

        WriteableBitmap wbm2 = new WriteableBitmap(mai, null);
        string tempjpeg = "tempmedicalertinfo";



        // create a virtual store and file stream. check for duplicate tempjpeg files.
        var mystore = IsolatedStorageFile.GetUserStoreForApplication();
        if (mystore.FileExists(tempjpeg))
        {
            mystore.DeleteFile(tempjpeg);
        }


        IsolatedStorageFileStream myfilestream = mystore.CreateFile(tempjpeg);

        wbm2.SaveJpeg(myfilestream, 500, 500, 0, 100);
        myfilestream.Close();

        // create a new stream from isolated storage, and save the jpeg file to the media library on windows phone.
        myfilestream = mystore.OpenFile(tempjpeg, FileMode.Open, FileAccess.Read);

        // save the image to the camera roll or saved pictures album.
        MediaLibrary library = new MediaLibrary();

        // save the image to the saved pictures album.
        try
        {
            Picture pic = library.SavePictureToCameraRoll("mai.jpg", myfilestream);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }


        myfilestream.Close();

Update:

Stack Trace of the error:

   at Microsoft.Xna.Framework.Helpers.ThrowExceptionFromErrorCode(ErrorCodes error)
   at Microsoft.Xna.Framework.Media.MediaLibrary.SavePicture(String name, Stream source)
   at PB.MASetup.saveImage_Click(Object sender, EventArgs e)
   at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args)
   at Microsoft.Phone.Shell.ApplicationBarIconButton.ClickEvent()
   at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent()
   at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand)
   at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand)

2条回答
Deceive 欺骗
2楼-- · 2019-04-13 02:56

The problem is that the streams are positioned byte data. So before you can pass your stream to the media library you must seek it back to the begin. That will solve your problem. Here is an example: (btw it's a good practice to use the using structure for every IDisposable object)

using (MemoryStream stream = new MemoryStream())
{
    WriteableBitmap bitmap = new WriteableBitmap(LayoutRoot, null);
    bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
    stream.Seek(0, SeekOrigin.Begin);

    using (MediaLibrary mediaLibrary = new MediaLibrary())
        mediaLibrary.SavePicture("Picture.jpg", stream);
}
MessageBox.Show("Picture Saved...");
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-13 02:58

After a lot of breaking my head I found that my problem was the missing capability in WMAppManifest.xml

  <Capability Name="ID_CAP_MEDIALIB" />

The error message was so vague that i had to waste so much of my time to figure this out.

查看更多
登录 后发表回答