简体   繁体   English

C#WP8.1:将图像从图库转换为base64

[英]C# WP8.1:Convert image from gallery to base64

I'm having a little trouble developing my app. 我在开发应用程序时遇到了一些麻烦。 What I'm trying to do now is that let the user pick their picture from gallery(picture album) and display it. 我现在想做的是让用户从画廊(相册)中挑选图片并显示出来。 Furthermore, I want to convert that picture taken to Base64 string. 此外,我想将拍摄的照片转换为Base64字符串。 Right now I've successfully pull and display picture from gallery, is there anyway I can convert that picture to base64 string. 现在,我已经成功地从图库中提取并显示图片了,无论如何我都可以将图片转换为base64字符串。

Here's the code how I grab and display 这是我抓取并显示的代码

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;
        }
    }

See this code below help . 请参阅帮助下面的代码。 I hope there are multiple ways to do this . 我希望有多种方法可以做到这一点。 The link might help you 该链接可能对您有帮助

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM