为什么在 C# 中调用 AForge 库函数会导致 AccessViolationException?
以下代码会导致访问冲突异常 - 这是为什么?我可以从 unsafe
块内部调用 AForge 过滤器吗?
unsafe
{
BitmapData bmd = ThresholdedImage.LockBits(boundingR, ImageLockMode.ReadWrite, ThresholdedImage.PixelFormat);
BitmapData bmda = intersectResult.LockBits(
new Rectangle(0, 0, intersectResult.Width, intersectResult.Height),
ImageLockMode.ReadWrite,
intersectResult.PixelFormat);
intersectResult = intersectFilter.Apply(bmd); //causes exception here
ImageStatistics st = new ImageStatistics(intersectResult);
area = st.PixelsCountWithoutBlack;
intersectResult.UnlockBits(bmda);
ThresholdedImage.UnlockBits(bmd);
}
The following code causes access violation exception - why is that? Can I call AForge filters from inside unsafe
block?
unsafe
{
BitmapData bmd = ThresholdedImage.LockBits(boundingR, ImageLockMode.ReadWrite, ThresholdedImage.PixelFormat);
BitmapData bmda = intersectResult.LockBits(
new Rectangle(0, 0, intersectResult.Width, intersectResult.Height),
ImageLockMode.ReadWrite,
intersectResult.PixelFormat);
intersectResult = intersectFilter.Apply(bmd); //causes exception here
ImageStatistics st = new ImageStatistics(intersectResult);
area = st.PixelsCountWithoutBlack;
intersectResult.UnlockBits(bmda);
ThresholdedImage.UnlockBits(bmd);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试图找出这个问题。
你能确认你的boundingR区域确实不代表整个图像吗?
您会收到此异常,因为在 AForge 端的某个时刻,有一个非托管字节副本错误地使用了完整图像的
Stride
值。我相信从
BaseInPlacePartialFilter
派生的过滤器可以处理图像区域,但基于BaseInPlaceFilter
的过滤器很可能会出现问题。如果可行,您可以在整个图像上应用滤镜,然后仅复制回您感兴趣的区域......
I tried to track down this issue.
Can you confirm that your boundingR region does not indeed represent the whole image ?
You will get this exception because at some point on AForge side there is an unmanaged copy of bytes that mistakenly uses the
Stride
value of the full image.I believe filters that derive from
BaseInPlacePartialFilter
will work with image regions, but filters based onBaseInPlaceFilter
will most likely have the issue.If it's feasible you may apply the filter on the whole image and then copy back just the region you are interested in…