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

查看:257
本文介绍了在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);*/
        }
    }

在屏幕外的最后一行抛出异常.SaveAsync.

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

上述错误的堆栈跟踪为:

The stack trace for the above error is:

在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在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天全站免登陆