在 UWP 中保存图像时访问被拒绝.访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED)) [英] Access denied while saving image in UWP.Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

查看:79
本文介绍了在 UWP 中保存图像时访问被拒绝.访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 10 SDK 上开发通用 Windows 应用程序,以在图像中识别的面部上绘制矩形.

I am working on universal windows app on Windows 10 SDK to draw rectangle on faces recognized in the image.

我正在使用 Win2D 编辑图片并在其上绘制矩形.我能够从图片库中读取文件,但是当我在编辑后尝试保存图像时出现以下错误:

I am using Win2D to edit the pictures and draw rectangle on it. I am able to read files from the Pictures library but when I try to save the image after editing it gives the following error:

访问被拒绝.(来自 HRESULT 的异常:0x80070005(E_ACCESSDENIED))

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

以下是我在图片上绘制矩形的方法:

The following is the method I used to draw rectangle on the image:

private async void DrawRect()
    {
        CanvasDevice device = CanvasDevice.GetSharedDevice();
        CanvasBitmap bitmap = null;
        CanvasRenderTarget offscreen = null;

        Windows.Storage.StorageFile photofile = await KnownFolders.PicturesLibrary.GetFileAsync("image.jpg");

        if(photofile != null)
        {
            using (var stream = await photofile.OpenReadAsync())
            {
                bitmap = await CanvasBitmap.LoadAsync(device, stream);
            }
        }

        if(bitmap != null)
        {
            offscreen = new CanvasRenderTarget(device, bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height, 96);
            using (var ds = offscreen.CreateDrawingSession())
            {
                ds.Clear(Colors.Transparent);
                ds.DrawImage(bitmap);
                ds.DrawRectangle(25, 35, 270, 352, Colors.Blue,4);
            }

            var photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("image2.jpg", CreationCollisionOption.ReplaceExisting);                        

            if (photofile != null)
            {
                await offscreen.SaveAsync(photofile.Path);
            }              

            //await offscreen.SaveAsync(photoFile.Path);*/
        }
    }

在最后一行 offscreen.SaveAsync 处抛出异常.

The exception is thrown at the last line offscreen.SaveAsync.

上述错误的堆栈跟踪是:

The stack trace for the above error is:

在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)在 System.Runtime.CompilerServices.TaskAwaiter.GetResult()在IdentifyFacesApp.IdentifiedFaces.d__5.MoveNext()

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at IdentifyFacesApp.IdentifiedFaces.d__5.MoveNext()

我已经设置了访问 appmanifest 文件中图片文件夹的权限.

I have set the permissions for accessing the pictures folders in the appmanifest file.

是否需要设置一些额外的权限才能将图像保存在磁盘中.

Do I need to set some additional permissions to save the image in the disk.

当我尝试将图像保存在任何其他位置时,也会发生同样的错误.

The same error occurs when I tried to save the image in any other location.

推荐答案

尝试通过流而不是路径访问文件:

Try to access the file by the stream, not the path:

var photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("image2.jpg", CreationCollisionOption.ReplaceExisting);

if (photofile != null)
{
    using (var stream = await photofile.OpenAsync(FileAccessMode.ReadWrite))
    {
        await offscreen.SaveAsync(stream, CanvasBitmapFileFormat.Jpeg);
    }
}

在 UWP 中,如果您通过路径访问文件,在许多情况下您可能会访问被拒绝,这应该通过 StorageFile 完成.

In UWP if you access files via path, you will likely get Access Denied in many cases, it should be done via StorageFile.

这篇关于在 UWP 中保存图像时访问被拒绝.访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆