简体   繁体   中英

How to convert image to base64

I want to convert an image to base64 in order to store it in Sqlite using with C#, but this code not suitable for windows 8.1 apps:

public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}

I believe you are asking for Windows Store APIs for this problem:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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