开发者

How does Marshal.ReadInt32 etc. differ from unsafe context and pointers?

开发者 https://www.devze.com 2023-03-23 11:26 出处:网络
Particularly: Is Marshal safer? Are pointers faster? int pixel = Marshal.ReadInt32(bitmapData.Scan0, x * 4 + y * bi开发者_JS百科tmapData.Stride);

Particularly: Is Marshal safer? Are pointers faster?

int pixel = Marshal.ReadInt32(bitmapData.Scan0, x * 4 + y * bi开发者_JS百科tmapData.Stride);
int pixel = ((int*)bitmapData.Scan0)[x + y * bitmapData.Stride / 4];


There is no difference. If you look at the code from Marshal.ReadInt32 you will see it uses pointers to perform the same thing.

The only 'benefit' with Marshal is that you not have to explicitly allow unsafe code. IIRC, you also require FullTrust to run unsafe code, so that may be a consideration.


I personally prefer using Marshal mostly because I shun unsafe code. As to which is faster, I'm not sure but I am certain that operating pixel by pixel is liable to be slow however you do it. Much better is to read an entire scanline into a C# array and work on that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号