如何将图像转换为base64string Xamarin形式?

时间:2018-08-19 02:41:18

标签: c# image xamarin xamarin.forms blob

我想将图像转换为 base64string 。我想将 base64string 保存到我的数据库中。我可以获取图像的路径。如何从路径中获取图像并将图像转换为 base64string ,以及需要保存什么 datatype 以便将 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");
   }

0 个答案:

没有答案