c# - 如何将图像转换为 base64string Xamarin 表单?
问题描述
我想将我的图像转换为base64string。我想将base64string保存到我的数据库中。我可以得到我的图像的路径。如何从我的路径中获取图像并将图像转换为base64string以及我需要放置什么数据类型才能将base64string保存到我的数据库中是BLOB吗?
try
{
var cafNo = entCafNo.Text;
var time = tpTime.Time;
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", "No Camera Available", "Ok");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(
new StoreCameraMediaOptions
{
SaveToAlbum = true,
Name = cafNo + "_IMG_01.jpg"
}
);
// provide read access to the file
FileStream fs = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
string _base64String = Convert.ToBase64String(ImageData);
entPhoto1Url.Text = _base64String;
}
catch(Exception ex)
{
await DisplayAlert("Error", ex.Message, "OK");
}
解决方案
推荐阅读
- python - Matlab 和 Python 中的大小函数
- android - 设置开始时间以监控地理围栏
- javascript - 如何在 React 中以编程方式构建对象数据的路径
- c++ - 这个片段中发生了什么关于 n 在 pc[i] 中的表示
- jquery - 如何使用 peerDependencies 解决 NPM 中的依赖地狱
- c# - 如何在 WPF/C# 中绘制多个 RectangleGeometry.Rect 并为其设置动画
- php - 我应该如何建立表之间的关系?
- ibm-cloud-private - IBM ICP Manegment-Ingress
- python - 有条件地创建熊猫列的最快方法
- informatica - 如何在 informatica 开发人员中不使用聚合器执行聚合