使用WriteableBitmapEx访问像素颜色时的AccessViolationException

时间:2013-04-19 16:17:25

标签: c# windows-8 writeablebitmapex

我正在使用WriteableBitmapEx库来编辑使用Windows 8 Pro的平板电脑摄像头拍摄的图像。我每次调用GetPixel()函数时都会收到AccessViolationException,这里是代码:

Windows.Media.Capture.MediaCapture captureMgr = new MediaCapture();
await captureMgr.InitializeAsync();

IRandomAccessStream memoryStream = new InMemoryRandomAccessStream();
await captureMgr.CapturePhotoToStreamAsync(imageProperties, memoryStream);
await memoryStream.FlushAsync();
memoryStream.Seek(0);

WriteableBitmap tmpImage = new WriteableBitmap(1, 1); 
tmpImage.SetSource(memoryStream);
tmpImage.GetPixel(1, 1); // An AccessViolationException occurs.

我做错了什么?

1 个答案:

答案 0 :(得分:4)

尝试使用内置方法来创建WriteableBitmap

WriteableBitmap tmpImage = await BitmapFactory.New(1, 1).FromStream(memoryStream);  
tmpImage.GetPixel(1, 1);

这应确保您的图像在被访问之前已加载到WriteableBitmap中。