C# 如何将图像转换为base64
我想将图像转换为base64,以便使用with C#将其存储在Sqlite中,但此代码不适用于windows 8.1应用程序:C# 如何将图像转换为base64,c#,windows-8.1,C#,Windows 8.1,我想将图像转换为base64,以便使用with C#将其存储在Sqlite中,但此代码不适用于windows 8.1应用程序: public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms,
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;
}
}
我相信您正在要求Windows应用商店API来解决此问题:
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);
}
公共异步任务ImageToBase64(存储文件MyImageFile)
{
Stream ms=wait MyImageFile.OpenStreamForReadAsync();
字节[]图像字节=新字节[(int)ms.Length];
ms.Read(imageBytes,0,(int)ms.Length);
返回Convert.tobase64字符串(imageBytes);
}
为什么不合适?您遇到了什么问题?namespace System.Drawing.Imaging.ImageFormat不存在更像是您在项目中缺少了一个引用。。。