2016-12-03 41 views
0

我爲uwp編寫應用程序imageControl.Source(C#UWP)

我有PCL,其中是打開相機,拍照並保存它的方法。

這是PCL中的方法代碼。

public async void PhotoTake() 
    { 
     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) 
     { 

      return; 
     } 
     StorageFolder destinationFolder = 
     await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists); 
     await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting); 
     await photo.DeleteAsync(); 

     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); 


    } 

在xaml中我有圖像。我需要在此圖像中顯示照片。

當我明白我需要寫水木清華這樣imageControl.Source = bitmapSource;

但我的,當我寫我有 Error CS0103 The name 'bitmapSource' does not exist in the current context

這裏是我的xaml.cs文件

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 

     CameraOpening cam = new CameraOpening(); 
     cam.PhotoTake(); 
     imageControl.Source = bitmapSource; 

    } 


} 

我怎麼hadle這個錯誤?

感謝的

+1

人,你有沒有試着挖成C#第一? –

回答

3

bitmapSource是你的方法內部的局部變量。你無法從外部訪問它。

方法的返回類型更改從voidTask<SoftwareBitmapSource>和創建位圖後刪除映像文件:

public async Task<SoftwareBitmapSource> PhotoTake() 
{ 
    var captureUI = new CameraCaptureUI(); 
    captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; 
    captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200); 

    var photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo); 
    var bitmapSource = new SoftwareBitmapSource(); 

    if (photo != null) 
    { 
     var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(
      "ProfilePhotoFolder", CreationCollisionOption.OpenIfExists); 

     await photo.CopyAsync(folder, "ProfilePhoto.jpg", 
      NameCollisionOption.ReplaceExisting); 

     using (var stream = await photo.OpenAsync(FileAccessMode.Read)) 
     { 
      var decoder = await BitmapDecoder.CreateAsync(stream); 
      var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); 
      var softwareBitmapBGR8 = SoftwareBitmap.Convert(
       softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); 

      await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); 
     } 

     await photo.DeleteAsync(); 
    } 

    return bitmapSource; 
} 

然後等待方法調用。因爲這不能在頁面的構造器來完成,你可以做一個Loaded事件處理程序:

public MainPage() 
{ 
    this.InitializeComponent(); 

    Loaded += async (o, e) => 
    { 
     var cam = new CameraOpening(); 

     imageControl.Source = await cam.PhotoTake(); 
    }; 
}