使用不安全的方法获取像素值和位置时尝试读取或写入受保护的内存错误 [英] Attempted to read or write protected memory error when unsafe method to get pixel values and position is used

查看:57
本文介绍了使用不安全的方法获取像素值和位置时尝试读取或写入受保护的内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow,我正在尝试查找最后一个不是黑色的像素,然后从那里开始创建一个矩形,为此我使用了此功能,我使用了不安全的方法来获取像素,并且出现了这个错误:
{尝试读取或写入受保护的内存.这通常表明其他内存已损坏.}".我不是一个经验丰富的编程人员,而Maby则经验丰富的眼睛会看到问题,有人可以告诉我该错误的原因是什么以及解决该问题的方法吗?

Hellow, i`m trying to find the last pixel that is not black and starting from there i create a rectangle and for that i use this function, i used the unsafe method to get pixels and i get this error :
"{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}". I`m not an experienced programer, maby a more experienced eye will see the problem can someone tell me what is the cause of that error and a way to solve it?

public unsafe static Rectangle getRectangle(Bitmap b)
        {
            Rectangle rct = new Rectangle();
            rct.Width = 40;
            rct.Height = 40;
            int x = 0;
            int y = 0;
            BitmapData bmd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
           
            int pixelSize = GetBitsPerPixel(b.PixelFormat);//gets the number of bits per pixel

            for (int j = 0; j < bmd.Height; j++)
            {
                byte* row = (byte*)bmd.Scan0 + (j * bmd.Stride);
                for (int i = 0; i < bmd.Stride; i++)
                {
                    int offSet = i * pixelSize;
                   
                    byte blue = row[offSet];//here i get the error!!!!
                    byte green = row[offSet + 1];
                    byte red = row[offSet + 2];
                    
                    if ((green > 0) && (blue> 0) && (red > 0))
                    {
                      
                        x = i;
                        y = j;
                    }
                 }
            }
          
            rct.X = x;
            rct.Y = y;
            return rct;

        }

推荐答案

有很多问题.
跨度表示每行在内存中占用多少字节.但是要查找所有像素,您仍然应该使用bmp.Width,而不是for中的跨度.

就您而言,如果图像为24位,则步幅将比宽度大3倍.
另外,您将图像读取为24位rgb(一个字节代表红色,一个字节代表绿色,一个字节代表蓝色),但是与此同时,您从未要求图像为24位格式.它可以是16位,8位或32位.
最简单的解决方案是考虑图像始终为24位,然后在LockBits上使用24位格式(并且您无需检查BitsPerPixel ...它将为24).
There are many problems.
The stride means how many bytes each line occupies in memory. But to look for all pixels, you should still use the bmp.Width, not the stride in the for.

In your case, if the image is 24 bits you will have a stride 3 times larger than your width.
Also, you read the image as a 24 bit rgb (one byte for red, one for green, one for blue), but at the same time you never asked the image to be in 24-bits format. It can very well be in 16-bit, 8-bit or 32-bit.
The easiest solution is to consider the image is always 24-bits, but then use the 24bit format on the LockBits (and you will not need to check the BitsPerPixel... it will be 24).


这篇关于使用不安全的方法获取像素值和位置时尝试读取或写入受保护的内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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