UnauthorizedAccessException使用AsRandomAccessStream [英] UnauthorizedAccessException using AsRandomAccessStream

查看:642
本文介绍了UnauthorizedAccessException使用AsRandomAccessStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级我的项目从8赢赢到8.1,我试图采取一些在SDK中的新功能。其中之一是新的 AsRandomAccessStream 扩展方法。 。我遇到的问题是,当我使用它,我发现了一个未经授权的访问异常




例外:捉住:MemoryStream的内部缓冲器不能访问。
(System.UnauthorizedAccessException的)在
System.UnauthorizedAccessException的被抓的MemoryStream的
内部缓冲区无法访问。时间:2014年3月11日上午10点23分11秒
主题:[4308]




 的BitmapImage形象=新的BitmapImage(); 
变种的ImageStream =新的MemoryStream(imageBytes以byte []);
image.SetSource(imageStream.AsRandomAccessStream());




  • imageBytes是有效的字节[]

  • 的ImageStream是一个有效的MemoryStream

  • imageStream.Position = 0



任何想法?


解决方案

今天我遇到了这个问题,对我来说,它表现为一个API错误/不一致的情况。



在.NET 4中,调用MemoryStream.GetBuffer()需要特定构造的使用(见的 https://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx )。更具体地,将MemoryStream的缓冲器必须被标记为可曝光



现在,AsRandomAccessStream()调用MemoryStream.GetBuffer()。然而,在Win8.1,设置一个MemoryStream的揭露能力构造丢失。因此,当您创建的MemoryStream,使用默认的空构造函数,然后调用write()。



因此,我认为这应该工作。

 的BitmapImage形象=新的BitmapImage(); 
变种的ImageStream =新的MemoryStream();
imageStream.Write(yourdata,0,yourdata.Length);
image.SetSource(imageStream.AsRandomAccessStream());


I just upgraded my project from Win 8 to Win 8.1 and I'm trying to take advantage of some of the new features in the SDK. One of those is the new AsRandomAccessStream extension method. The problem I'm having is when I use it, I'm getting an Unauthorized Access Exception.

Exception:Caught: "MemoryStream's internal buffer cannot be accessed." (System.UnauthorizedAccessException) A System.UnauthorizedAccessException was caught: "MemoryStream's internal buffer cannot be accessed." Time: 3/11/2014 10:23:11 AM Thread:[4308]

BitmapImage image = new BitmapImage();
var imageStream = new MemoryStream(imageBytes as byte[]); 
image.SetSource(imageStream.AsRandomAccessStream());

  • imageBytes is a valid byte[]
  • imageStream is a valid MemoryStream
  • imageStream.Position = 0

any thoughts?

解决方案

I encountered this problem today and to me, it appears as an API bug/inconsistency.

In .NET 4, calls to MemoryStream.GetBuffer() require the usage of certain constructors (see https://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx). More specifically, the buffer of the MemoryStream must be marked as exposable.

Now, AsRandomAccessStream() calls MemoryStream.GetBuffer(). However, in Win8.1, the constructor for setting the expose-ability of a MemoryStream is missing. Therefore, when you create the MemoryStream, use the default empty constructor and then call Write().

Thus, I think this should work.

BitmapImage image = new BitmapImage();
var imageStream = new MemoryStream();
imageStream.Write(yourdata, 0, yourdata.Length); 
image.SetSource(imageStream.AsRandomAccessStream());

这篇关于UnauthorizedAccessException使用AsRandomAccessStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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