[英]C# WP8.1:Convert image from gallery to base64
我在開發應用程序時遇到了一些麻煩。 我現在想做的是讓用戶從畫廊(相冊)中挑選圖片並顯示出來。 此外,我想將拍攝的照片轉換為Base64字符串。 現在,我已經成功地從圖庫中提取並顯示圖片了,無論如何我都可以將圖片轉換為base64字符串。
這是我抓取並顯示的代碼
private void picprofile_Tapped(object sender, TappedRoutedEventArgs e)
{
CoreApplicationView view;
String ImagePath;
view = CoreApplication.GetCurrentView();
ImagePath = string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
}
private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
CoreApplicationView view;
view = CoreApplication.GetCurrentView();
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile storageFile = args.Files[0];
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
picprofile.ImageSource = bitmapImage;
}
}
請參閱幫助下面的代碼。 我希望有多種方法可以做到這一點。 該鏈接可能對您有幫助
public async Task<string> ImageToBase64(StorageFile MyImageFile)
{
Stream ms = await MyImageFile.OpenStreamForReadAsync();
byte[] imageBytes = new byte[(int)ms.Length];
ms.Read(imageBytes, 0, (int)ms.Length);
return Convert.ToBase64String(imageBytes);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.