gpt4 book ai didi

c# - 如何使用 UWP 将图像保存到 azure mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 11:26:51 24 4
gpt4 key购买 nike

我使用以下方式拍摄了图片cameracaptureui 和我在图像控制中有图像。现在的问题是如何将捕获的图像保存到我的数据库中?通常我通过将图像转换为字节以 Windows 形式完成此任务。但现在 UWP 有点困惑。提前致谢

我尝试过:`

 private async void button_Copy_Click(object sender, RoutedEventArgs e)
{
//create camera instance with camera capture ui
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null)
{
// User cancelled photo capture
return;
}
//return the captured results to fram via bitmap
IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
imageControl.Source = bitmapSource;
}

最佳答案

将图像转换为Base64并将其保存到mysql数据库。将捕获的图像保存到应用程序本地文件夹并将其转换为Base64

C# 代码:

using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

private async void btn_Click(object sender, RoutedEventArgs e)
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + "your_image_name.png"));
string base64image = await _encodeToBase64(file.Path);
}

public async Task<string> _encodeToBase64(string filePath)
{
string encode = String.Empty;
if (!string.IsNullOrEmpty(filePath))
{
StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
IBuffer buffer = await FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
encode = Convert.ToBase64String(fileContent);
}
return encode;
}

关于c# - 如何使用 UWP 将图像保存到 azure mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com