convert image into base64 in wp8

前端 未结 1 1357
温柔的废话
温柔的废话 2021-01-26 11:25

I have an image taken from my phone gallery, like below:

private void StackPanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
    PhotoChooser         


        
1条回答
  •  隐瞒了意图╮
    2021-01-26 11:41

    Simply convert the byte[] to a base64 string:

    byte[] bytearray = null;
    
    using (MemoryStream ms = new MemoryStream())
    {
        if (imgphotochoser.Source != null)
        {
            WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)imgphotochoser.Source);
    
            wbitmp.SaveJpeg(ms, 46, 38, 0, 100);
            bytearray = ms.ToArray();
        }
    }
    string str = Convert.ToBase64String(bytearray);
    

    Base64 to byte[]:

    byte[] fileBytes = Convert.FromBase64String(s);
    
    using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
    {
        ms.Write(fileBytes, 0, fileBytes.Length);
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.SetSource(ms);
        return bitmapImage;
    }
    

    0 讨论(0)
提交回复
热议问题