Guardar un mapa de bits que se pueda escribir

"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 Error inesperado 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.

Actualizar:

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();

Actualizar:

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)

preguntado el 09 de marzo de 12 a las 13:03

In which context you call this? Are wbm.PixelWidth and wbm.PixelHeight have right values? -

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 -

Agregué tu código a Loaded event and all works -

@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) {... -

Me funciona con Button y ApplicationBarButton on Device y Emulator. -

2 Respuestas

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...");

respondido 09 mar '12, 15:03

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.

Respondido 22 Abr '12, 09:04

DAMN! After banging my head on my pc for 2 days, I got to know this. Where were you??? Thanks :-) - Noob

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.