2012-07-17 56 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 ...)