2012-07-17 76 views
0

當我使用此功能將圖像(從網上抓取)保存到IsolatedStorage時,我發現文件大小比我從網頁瀏覽器保存的大。如何獲得bitmapimage(jpg/png)的長度?

public static bool CreateImageFile(string filePath, BitmapImage bitmapImage) 
{ 
     //StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative)); 

     using (isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      string directoryName = System.IO.Path.GetDirectoryName(filePath); 
      if (!string.IsNullOrEmpty(directoryName) && !isolatedStorage.DirectoryExists(directoryName)) 
      { 
       isolatedStorage.CreateDirectory(directoryName); 
      } 

      if (isolatedStorage.FileExists(filePath)) 
      { 
       isolatedStorage.DeleteFile(filePath); 
      } 

      //bitmapImage 

      using (IsolatedStorageFileStream fileStream = isolatedStorage.OpenFile(filePath, FileMode.Create, FileAccess.Write)) 
      { 
       bitmapImage.CreateOptions = BitmapCreateOptions.None; 
       WriteableBitmap wb = new WriteableBitmap(bitmapImage); 
       wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100); 
       fileStream.Close(); 
      } 
     } 
     return true; 
} 

是否可以使用WriteableBitmap.SaveJpeg(...)保存png圖像? 是否有任何函數來獲取BitmapImage的長度?

+0

你爲什麼要保存SaveJpeg方法PNG文件?看看PNG編碼器http://blogs.msdn.com/b/nikola/archive/2009/03/04/silverlight-super-fast-dymanic-image-generation-code-revisited.aspx – 2012-07-17 13:26:19

回答

0

轉換爲字節數組

byte[] data; 
using (MemoryStream ms = new MemoryStream() 
{ 
    bitmapImage.SaveJpeg(ms, LoadedPhoto.PixelWidth, LoadedPhoto.PixelHeight, 0, 95); 
    ms.Seek(0, 0); 
    data = new byte[ms.Length]; 
    ms.Read(data, 0, data.Length); 
    ms.Close(); 
} 

然後剛剛得到的字節數組的大小,並轉換爲更合理(KB,MB ...)