2016-06-28 120 views
0

我使用 拍攝了照片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; 
    } 

回答

1

轉換圖片到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://stackoverflow.com/questions/18156706/get-local-folders-from-app-winrt)。 – Irfan

+0

將拍攝的圖像保存到應用程序本地狀態文件夾。 – Irfan

相關問題