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

问题描述:

我使用 拍摄了照片cameracaptureui,并且我在图像控制中拥有图像。 现在的问题是如何将捕获的图像保存到我的数据库。 通常我通过将图像转换为Byets在Windows窗体中完成此任务。但现在在UWP中有点混乱。 由于事先如何使用UWP将图像保存到azure mysql数据库

我曾尝试:`

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数据库。将捕获的图像保存到Application Local Folder并将其转换为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; 
    } 
在新的URI
+0

我应该提供什么样的路径? –

+0

@ SheikhMuhammadAmaan-Ullah这是你的应用程序本地文件夹阅读[这里](http://*.com/questions/18156706/get-local-folders-from-app-winrt)。 – Irfan

+0

将拍摄的图像保存到应用程序本地状态文件夹。 – Irfan