保存WriteableBitmap

时间:2012-03-09 13:40:02

标签: windows-phone-7 windows-phone-7.1

“mai”是包含图像,文本和小图像的网格名称。我正在关注一篇关于能够通过将其作为WriteableBitmap(带有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());
    }

当我在模拟器上以调试模式运行它时,我收到意外错误消息。我还将此应用程序部署到我的手机上(并将其与计算机断开连接)并收到同样的错误。

基本上我正在尝试保存从相机胶卷中拾取的裁剪图像,并在其上面叠加一些文字。它喜欢将这个“新”图像保存到相机胶卷中。

更新

我也做了同样的结果:

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

更新

错误的堆栈跟踪:

   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 个答案:

答案 0 :(得分:10)

问题是流是定位字节数据。因此,在将流传递到媒体库之前,您必须将其重新发送到开头。这将解决您的问题。这是一个例子:(顺便说一句,对每个IDisposable对象使用using结构是一个很好的做法)

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

答案 1 :(得分:5)

经过多次打破我发现我的问题是WMAppManifest.xml中缺少的功能

  <Capability Name="ID_CAP_MEDIALIB" />

错误信息是如此模糊,我不得不浪费我的大部分时间来解决这个问题。