2

"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)
webdad3
  • 8,893
  • 30
  • 121
  • 223
  • In which context you call this? Are `wbm.PixelWidth` and wbm.PixelHeight have right values? – Ku6opr Mar 09 '12 at 13:46
  • I just checked and the width value = 456 and the height value = 535 - It appears that it grabs what ever the size of the grid is:mai – webdad3 Mar 09 '12 at 13:51
  • I added your code to `Loaded` event and all works – Ku6opr Mar 09 '12 at 13:59
  • @Ku6opr - can you give me an example? The Loaded event to what? My code is in a button click event: private void saveImage_Click(object sender, EventArgs e) {... – webdad3 Mar 09 '12 at 14:02
  • It works for me with `Button` and `ApplicationBarButton` on `Device` and `Emulator`. – Ku6opr Mar 09 '12 at 14:07
  • OK well then I guess it is a setting I'm missing? Can you post what references you have? What about Capability's in the WMAppManifest? – webdad3 Mar 09 '12 at 14:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8714/discussion-between-ku6opr-and-jeff-v) – Ku6opr Mar 09 '12 at 14:11

2 Answers2

10

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...");
Loránd Biró
  • 856
  • 6
  • 7
5

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.

moonlightdock
  • 1,358
  • 13
  • 14