Unsafe Per Pixel access, 30ms access for 1756000 pixels

前端 未结 5 1537
轮回少年
轮回少年 2020-12-28 10:42

So I\'ve been sharing some thoughts on the above topic title on my website about fast, unsafe pixel access. A gentlemen gave me a rough example of how he\'d do it in C++, bu

5条回答
  •  醉酒成梦
    2020-12-28 11:19

    Yes, you can do so by using unsafe code.

    BitmapData d = l.LockBits(new Rectangle(0, 0, l.Width, l.Height), ImageLockMode.ReadOnly,l.PixelFormat);
    IntPtr scan = d.Scan0;
    unsafe
    {
        byte* p = (byte*)(void*)scan;
        //dostuff               
    }
    

    Check out http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx for some basic examples of this kind of stuff. My code is based on that.

    Note: One of the reasons this will be much faster than yours is that you are separately comparing each channel instead of just comparing the entire byte using one operation. Similarly, changing PixelAt to give you a byte to facilitate this would probably give you an improvement.

提交回复
热议问题