2012-02-24 43 views
2

我希望創造一個函數,它接受一個BitmapImage,它在獨立存儲中保存爲JPEG本地的Windows Phone 7設備上:如何將位圖圖像保存爲Windows Phone 7設備上的JPEG圖像文件?

static public void saveImageLocally(string barcode, BitmapImage anImage) 
{ 
// save anImage as a JPEG on the device here 
} 

如何做到這一點?我假設我以某種方式使用了IsolatedStorageFile

謝謝。

編輯:

這裏是我迄今發現...誰能確認這是否是這樣做的正確方法?

static public void saveImageLocally(string barcode, BitmapImage anImage) 
    { 
     WriteableBitmap wb = new WriteableBitmap(anImage); 

     using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (var fs = isf.CreateFile(barcode + ".jpg")) 
      { 
       wb.SaveJpeg(fs, wb.PixelWidth, wb.PixelHeight, 0, 100); 
      } 
     } 
    } 

    static public void deleteImageLocally(string barcode) 
    { 
     using (IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      MyStore.DeleteFile(barcode + ".jpg"); 
     } 
    } 

    static public BitmapImage getImageWithBarcode(string barcode) 
    { 
     BitmapImage bi = new BitmapImage(); 

     using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (var fs = isf.OpenFile(barcode + ".jpg", FileMode.Open)) 
      { 
       bi.SetSource(fs); 
      } 
     } 

     return bi; 
    } 

回答

5

要保存它:

var bmp = new WriteableBitmap(bitmapImage); 
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     using (IsolatedStorageFileStream stream = storage.CreateFile(@"MyFolder\file.jpg")) 
     { 
      bmp.SaveJpeg(stream, 200, 100, 0, 95); 
      stream.Close(); 
     } 
    } 

是的,你在編輯添加的東西是什麼我做了它的工作原理之前:)。

1

這是我的代碼,但你可以從那裏乘neccesary點:

 var fileName = String.Format("{0:}.jpg", DateTime.Now.Ticks); 
     WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap(480, 552); 
     bmpCurrentScreenImage.Render(yourCanvas, new MatrixTransform()); 
     bmpCurrentScreenImage.Invalidate(); 
     SaveToMediaLibrary(bmpCurrentScreenImage, fileName, 100); 


    public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality) 
    { 
     using (var stream = new MemoryStream()) 
     { 
      // Save the picture to the Windows Phone media library. 
      bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality); 
      stream.Seek(0, SeekOrigin.Begin); 
      new MediaLibrary().SavePicture(name, stream); 
     } 
    } 
相關問題