2012-04-15 66 views
0

我試圖在將圖像保存到獨立存儲之前減小圖像的大小。我已經將它降低到最低分辨率(640x480)如何將字節數減少到100kb,而不是將它們以近乎1mb的速度出現。我該如何縮小圖像的尺寸

void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e) 
    { 

     string fileName = folderName+"\\MyImage" + savedCounter + ".jpg"; 

     try 
     { 

      // Set the position of the stream back to start 
      e.ImageStream.Seek(0, SeekOrigin.Begin); 

      // Save picture as JPEG to isolated storage. 
      using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) 
       { 

        // Initialize the buffer for 4KB disk pages. 
        byte[] readBuffer = new byte[4096]; 
        int bytesRead = -1; 

        // Copy the image to isolated storage. 
        while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0) 
        { 
         targetStream.Write(readBuffer, 0, bytesRead); 
        } 

       } 

      } 




     } 
     finally 
     { 
      // Close image stream 
      e.ImageStream.Close(); 
     } 

    } 
+0

通過壓縮。 – 2012-04-15 17:07:03

+0

如何壓縮? – 2012-04-15 17:56:12

回答

0

我一無所知的Windows Phone,但也許這種聯繫將幫助您: http://msdn.microsoft.com/en-us/library/ff769549(v=vs.92).aspx

1)加載JPEG 2)複製成位圖 3)保存爲質量設置位圖 http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.extensions.savejpeg(v=vs.92).aspx

+0

當我嘗試使用'var bitmapImage = new BitmapImage()保存時,我得到了無效的跨線程訪問。 bitmapImage.SetSource(e.ImageStream); WriteableBitmap wb = new WriteableBitmap(bitmapImage); IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();使用(IsolatedStorageFileStream myFileStream = myStore.CreateFile(fileName)) wb.SaveJpeg(myFileStream,10,10,0,70); }' – 2012-04-15 17:50:53

+0

就像我說的,我不知道任何關於windows phone:P – zgnilec 2012-04-15 20:39:26