2012-09-16 54 views
1

我有一個Windows 8程序,使用圖像選擇器,並在服務器上下載選定的圖像。 服務器提供了一個需要在base64string中轉換圖像的API。圖像必須小於7Mb。在Windows 8 Base64String 8

我使用下面的代碼:

FileOpenPicker openPicker = new FileOpenPicker(); 
openPicker.ViewMode = PickerViewMode.Thumbnail; 
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
openPicker.FileTypeFilter.Add(".jpg"); 
openPicker.FileTypeFilter.Add(".jpeg"); 
openPicker.FileTypeFilter.Add(".png"); 
StorageFile file = await openPicker.PickSingleFileAsync(); 
if (file != null) 
{ 
    // Application now has read/write access to the picked file 
    bitmap = new BitmapImage(); 
    byte[] buf; 
    using (var stream = await file.OpenStreamForReadAsync()) 
    { 
     buf = ReadToEnd(stream); 
    } 

    using (var stream = await file.OpenAsync(FileAccessMode.Read)) 
    { 
     base64String = Convert.ToBase64String(buf); 
     bitmap.SetSource(stream); 
    } 
} 

和位圖進入到服務器。 但存在一個問題:例如,位圖大小比jpg的大小大得多。並且小jpgs都不會去服務器,因爲它們的位圖版本大於7 Mb。

我可以將圖像轉換爲base64string而不將其轉換爲位圖嗎?

回答

0

在此代碼中,您讀取圖像(以jpeg編碼)並將其轉換爲基本64字符串。 在不縮小圖像大小的情況下,不能縮小基底64的大小。

爲此,您可以使用BitmapEncoder/Decoder並將圖像大小調整爲較小。

Regards