如何获取位图图像并将其保存为Windows Phone 7设备上的JPEG图像文件?

我期待创建一个函数,它采用BitmapImage并将其作为JPEG保存在独立存储中的本地Windows Phone 7设备上:

 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; } 

要保存它:

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

是的,您在编辑中添加的内容正是我之前所做的:)它的工作原理。

这是我的代码,但您可以从那里获取必要的点:

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