c# - How can I save a captured picture from the user to the cosmos db in xamarin forms?
问题描述
It's supposed that I allow the user to capture a picture using his camera or pick one from the gallery. I have a property called picture in the model which it's supposed to have the path of the image. How can I save this image to the database and save its path in the property?
解决方案
如何将此图像保存到数据库并将其路径保存在属性中?
您可以将图像文件作为字节数组存储到数据库中。你是如何挑选或捕捉照片的?您可以使用Xam.Plugin.Media 插件来实现该功能。该库提供了相关的方法,可以帮助轻松保存图片。
从图库中挑选照片后,尝试使用MediaFile.Path
命令获取图像文件的路径。您可以使用GetStream()
方法获取流并将流转换为字节数组。然后将 byte[] 和路径字符串存储在数据库中。
var image_file = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
var Image_stream = image_file.GetStream();
image_file.Dispose();
//convert the image stream to byte array
private byte[] GetImageBytes(Stream stream)
{
byte[] ImageBytes;
using (var memoryStream = new System.IO.MemoryStream())
{
stream.CopyTo(memoryStream);
ImageBytes = memoryStream.ToArray();
}
return ImageBytes;
}
推荐阅读
- flutter - 颤振与 setInt 共享首选项问题
- python - Pybind11 - 转换抽象继承的类元素列表
- java - 如何检查自定义对象列表是否包含另一个列表中具有相同属性的所有元素?
- r - 如何使用 R 中的函数输入更改 URL 的一部分?
- ruby-on-rails - Rails 6 如何解决 ActionView::Template::Error (Webpacker 在 public/packs/manifest.json 中找不到应用程序
- javascript - 如何为小屏幕分辨率(Angular、Bootstrap)调整下拉搜索预览结果的大小
- java - org.hibernate.PropertyAccessException:无法通过 String 的反射设置字段值 [STRING] 值
- javascript - nodejs:如何在Windows上最小化或落后于其他应用程序时录制正在运行的进程的视频?
- javascript - 1.如果'selector'是一个Angular组件,那么验证它是这个模块的一部分
- c++ - 通过opengl中的纹理传递数组数据