2012-03-09 46 views
2

“mai”是包含图像,文本和小图像的网格名称。我正在关注一篇关于如何通过使其成为WriteableBitmap(使用UIelment)来添加图像的博文。保存WriteableBitmap

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) 
+0

在哪种情况下,你可以称之为? 'wbm.PixelWidth'和wbm.PixelHeight是否有正确的值? – Ku6opr 2012-03-09 13:46:52

+0

我刚刚检查过,宽度值= 456,高度值= 535 - 看起来它抓住了网格的大小:mai – webdad3 2012-03-09 13:51:54

+0

我将代码添加到'Loaded'事件并且所有作品 – Ku6opr 2012-03-09 13:59:07

回答

10

的问题是,流定位字节数据。因此,在将流传送到媒体库之前,您必须先找到它。这将解决你的问题。下面是一个例子:(顺便说一句这是使用使用结构为每个IDisposable的对象一个很好的做法)

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

很多打破我的头,我发现我的问题是在WMAppManifest.xml

缺少的能力后
<Capability Name="ID_CAP_MEDIALIB" /> 

错误信息太模糊了,我不得不浪费我很多时间来弄清楚。

+0

DAMN!在我的电脑上敲了2天后,我才知道这一点。你去哪了???谢谢 :-) – noob 2013-09-13 01:42:25