2012-03-09 50 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