首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 8.1如何关闭StorageFile?

Windows 8.1如何关闭StorageFile?
EN

Stack Overflow用户
提问于 2016-06-15 10:54:47
回答 1查看 195关注 0票数 0

我有个小问题。我使用grayScale过滤器创建图像,使用StorageFile。当我试图在第二个图像上设置过滤器时,问题就开始了。当我第一次拍摄并设置过滤器和它的好,第二次当我拍摄并试图设置过滤器时,我得到了错误消息:Access is denied. Exception from HRESULT: ...。第三次当我拍摄照片时,我可以设置过滤器,第四次我再次得到错误等等。我知道问题在于应用程序仍然使用相同的StorageFile和它的锁定,但我不知道如何关闭该文件。

在这里我创建了StorageFile文件:

代码语言:javascript
复制
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));
}

这是grayScale过滤器:

代码语言:javascript
复制
 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(); 
    }
}

在这一行中,我得到了错误:

代码语言:javascript
复制
using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-15 11:05:17

您需要在编写数据之后立即关闭filestream

代码语言:javascript
复制
using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    await RandomAccessStream.CopyAsync(imageStream, filestream);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37833428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档