2016-06-15 7 views
0

少し問題があります。私は、StorageFileを使ってgrayScaleフィルターで画像を作成しています。問題は、2番目の画像にフィルターを設定しようとすると始まります。初めて写真を撮ってフィルターをセットし、その写真を撮ってフィルターをセットしようとすると、エラーメッセージ:Access is denied. Exception from HRESULT: ...が表示されます。私は写真を撮るときに3回目、私はフィルタを設定することができます、4回目にはもう一度エラーがあります。私はその問題はアプリが同じStorageFileとそのロックを使用していることを知っていますが、そのファイルを閉じる方法はわかりません。Windows phone 8.1 StorageFileを閉じるには?

ここで私はStorageFileファイル作成:

async private void Capture_Photo_Click(object sender, RoutedEventArgs e) 
{ 
    ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); 
    InMemoryRandomAccessStream imageStream = new InMemoryRandomAccessStream(); 
    await newCapture.CapturePhotoToStreamAsync(imgFormat, imageStream); 
    BitmapDecoder dec = await BitmapDecoder.CreateAsync(imageStream); 
    BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(imageStream, dec); 
    string currentorientation = DisplayInformation.GetForCurrentView().CurrentOrientation.ToString(); 
    switch (currentorientation) 
    { 
     case "Landscape": 
      enc.BitmapTransform.Rotation = BitmapRotation.None; 
      break; 
     case "Portrait": 
      enc.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees; 
      break; 
     case "LandscapeFlipped": 
      enc.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees; 
      break; 
     case "PortraitFlipped": 
      enc.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees; 
      break; 
     default: 
      enc.BitmapTransform.Rotation = BitmapRotation.None; 
      break; 
    } 
    await enc.FlushAsync(); 
    string naziv = "IMG_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"; 
    naziv = naziv.Insert(12, "_"); 
    file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
     naziv, 
     CreationCollisionOption.ReplaceExisting); 
    var filestream = await file.OpenAsync(FileAccessMode.ReadWrite); 
    await RandomAccessStream.CopyAsync(imageStream, filestream); 
    BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); 
    var obj = App.Current as App; 
    obj.fileTransfer = file; 
    obj.ImageToEdit = bmpImage; 
    await newCapture.StopPreviewAsync(); 
    bmpImage = null; 
    await imageStream.FlushAsync(); 
    await filestream.FlushAsync(); 
    Frame.Navigate(typeof(EditImage)); 
} 

そして、これは、グレースケールフィルタである:この行で

private async void ConvertToGrayScale() 
{ 

    var obj = App.Current as App; 
    StorageFile file = obj.fileTransfer; 
    try { 
     if (obj.isCrooped == true && obj.writebleImg != null) { 
      file = await WriteableBitmapToStorageFile(obj.writebleImg); 
     } 
     else { 
      file = obj.fileTransfer; 
     } 
     BitmapDecoder decoder = null; 

     using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) 
     { 
      decoder = await BitmapDecoder.CreateAsync(stream); 

      // Get the first frame 
      BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); 

      // Save the resolution (will be used for saving the file later) 
      //dpiX = bitmapFrame.DpiX; 
      //dpiY = bitmapFrame.DpiY; 

      // Get the pixels 
      PixelDataProvider dataProvider = 
       await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, 
                BitmapAlphaMode.Premultiplied, 
                new BitmapTransform(), 
                ExifOrientationMode.RespectExifOrientation, 
                ColorManagementMode.ColorManageToSRgb); 

      byte[] pixels = dataProvider.DetachPixelData(); 

      // Create WriteableBitmap and set the pixels 
      WriteableBitmap srcBitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth, 
                 (int)bitmapFrame.PixelHeight); 

      using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream()) 
      { 
       await pixelStream.WriteAsync(pixels, 0, pixels.Length); 
      } 


      byte[] srcPixels = new byte[4 * srcBitmap.PixelWidth * srcBitmap.PixelHeight]; 

      using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream()) 
      { 
       await pixelStream.ReadAsync(srcPixels, 0, srcPixels.Length); 
      } 

      // Create a destination bitmap and pixels array 
      WriteableBitmap dstBitmap = 
        new WriteableBitmap(srcBitmap.PixelWidth, srcBitmap.PixelHeight); 
      byte[] dstPixels = new byte[4 * dstBitmap.PixelWidth * dstBitmap.PixelHeight]; 


      for (int i = 0; i < srcPixels.Length; i += 4) 
      { 
       double b = (double)srcPixels[i]/255.0; 
       double g = (double)srcPixels[i + 1]/255.0; 
       double r = (double)srcPixels[i + 2]/255.0; 

       byte a = srcPixels[i + 3]; 

       double e = (0.21 * r + 0.71 * g + 0.07 * b) * 255; 
       byte f = Convert.ToByte(e); 

       dstPixels[i] = f; 
       dstPixels[i + 1] = f; 
       dstPixels[i + 2] = f; 
       dstPixels[i + 3] = a; 

      } 

      // Move the pixels into the destination bitmap 
      using (Stream pixelStream = dstBitmap.PixelBuffer.AsStream()) 
      { 
       await pixelStream.WriteAsync(dstPixels, 0, dstPixels.Length); 
      } 
      dstBitmap.Invalidate(); 

      // Display the new bitmap 
      ImagePreview.Source = dstBitmap; 

     } 

    } 
    catch (Exception err) { 
    err.StackTrace.ToString(); 
    } 
} 

を私はエラーを得た:

using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) 
+0

私はあなたの前の質問にのみコードを投稿することを意味しました。キーワードを使用して、それは作業が完了したら自動的にファイルを閉じます。 (StorageFile file = obj.fileTransfer;){...} ' – Archana

+0

ありがとう、その作業 –

+0

' dstBitmap'をギャラリーに保存したいと思っていますか、 'dstBitmap'を' StoageFile'に変換しています – Archana

答えて

0

あなたはfilestreamをクローズする必要がありますあなたがデータを書いた直後。

using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite)) 
{ 
    await RandomAccessStream.CopyAsync(imageStream, filestream); 
} 
関連する問題