Skip to main content
 首页 » 编程设计

c#之使用 WriteableBitmapEx 访问像素颜色时出现 AccessViolationException

2024年02月01日32daizhj

我正在使用 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. 

我做错了什么?

请您参考如下方法:

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

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

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