2011-06-16 154 views
4

基本上我使用Visual Studio/Expression Blend來執行我的應用程序。其作品與用戶一樣可以選擇他/她想要編輯的圖片,編輯後用戶只需點擊保存按鈕,編輯後的圖片將保存在獨立存儲中,但我無法命令保存按鈕保存將圖像存入獨立存儲器,以便希望有人會提前通過一些示例代碼來幫助我。將圖像存儲到windows phone 7中的獨立存儲中

我試着用下面的代碼,但是當我按下保存按鈕時有一個空引用錯誤。我的想法是,當你按下保存時,應用程序不知道要將哪個圖像保存到隔離存儲器中,並且不能確定我的想法是否正確。任何人都可以幫助我這個。非常感謝。

private void btnSave_Click(object sender, RoutedEventArgs e) 
{ 
    String tempJPEG = "TempJPEG"; 

    var myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
    if (myStore.FileExists(tempJPEG)) 
    { 
     myStore.DeleteFile(tempJPEG); 
    } 

    IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG); 

    Uri uri = new Uri("TestImage.jpg", UriKind.Relative); 
    StreamResourceInfo sri = Application.GetResourceStream(uri); 

    BitmapImage bitmap = new BitmapImage(); 
    bitmap.CreateOptions = BitmapCreateOptions.None; 
    bitmap.SetSource(sri.Stream); 
    WriteableBitmap wb = new WriteableBitmap(bitmap); 

    Extensions.SaveJpeg(wb, myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
    myFileStream.Close(); 
+0

顯示保存代碼。 – 2011-06-16 13:21:49

回答

2

這是代碼

private void saveButtonClick(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (isf.FileExists("myImage.jpg")) 
       isf.DeleteFile("myImage.jpg"); 
      using (var isfs = isf.CreateFile("myImage.jpg")) 
      { 
       var bmp = new WriteableBitmap(myImageElement, 
           myImageElement.RenderTransform); 
       bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100); 
      } 
     } 
    } 
    catch (Exception exc) 
    { 
     MessageBox.Show(exc.Message); 
    } 
} 

這裏myImageElement的工作版本是在其中顯示圖像的圖像元素。

+0

嗨,你的這段代碼放在保存按鈕下面?意思是在你點擊保存按鈕後,圖像將存儲到獨立的存儲器中?順便說一下,圖像對象是什麼? – user801456 2011-06-16 13:45:30

+0

你可以將代碼放在任何你想要的地方,因爲你想保存圖像,你可以把它放在按鈕的ClickEvent處理程序中。通過[圖像對象]我的意思是你正在使用的圖像元素來顯示圖像 – 2011-06-17 05:20:32

+0

我厭倦了,但仍然錯誤。 bi.SetSource(pic); 你在哪裏申報你的照片? – user801456 2011-06-17 05:38:43

相關問題