首页 > 解决方案 > 如何将图像转换为 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");
   }

标签: c#imagexamarinxamarin.formsblob

解决方案


推荐阅读