2016-06-15 10 views
0

Ich habe ein kleines Problem. Ich erstelle ein Bild mit dem grayScale-Filter, indem ich StorageFile verwende. Das Problem beginnt, wenn ich versuche, das zweite Bild zu filtern. Das erste Mal, das ich das Bild mache und den Filter einstelle und es ok, das zweite Mal, wenn ich ein Bild mache und versuche, den Filter zu setzen, bekomme ich die Fehlermeldung: Access is denied. Exception from HRESULT: .... Das dritte Mal, wenn ich ein Bild mache, kann ich den Filter einstellen, das vierte Mal habe ich einen Fehler bekommen und so weiter. Ich weiß, dass das Problem ist, dass die App immer noch die gleiche StorageFile verwendet und gesperrt ist, aber ich weiß nicht, wie ich diese Datei schließe.Windows Phone 8.1 wie StorageFile zu schließen?

Hier erstellen i StorageFile Datei:

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

Und das graustufeninvertiert Filter:

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

Auf dieser Linie i Fehler bekam:

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

Ich wollte den Code nur in Ihrer vorherigen Frage, nicht als separate Frage, aber es ist in Ordnung. Verwenden Sie ein Schlüsselwort, um die Datei nach Abschluss der Arbeit automatisch zu schließen. 'using (StorageFile file = obj.fileTransfer;) {...}' – Archana

+0

Vielen Dank, es funktioniert –

+0

Ich nehme an, Sie möchten 'dstBitmap' in die Galerie speichern oder konvertieren Sie' dstBitmap' in 'StoageFile' – Archana

Antwort

0

Sie müssen die filestream schließen direkt nachdem du die Daten geschrieben hast.

Verwandte Themen