Like Marshal.ReadInt32 etc. Differ from insecure context and pointers?

In particular: is the marshal safe? Are pointers faster?

int pixel = Marshal.ReadInt32(bitmapData.Scan0, x * 4 + y * bitmapData.Stride); int pixel = ((int*)bitmapData.Scan0)[x + y * bitmapData.Stride / 4]; 
+6
source share
2 answers

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

The only “advantage” with Marshal is that you do not need to explicitly allow unsafe code. IIRC, you also need FullTrust to run unsafe code so that this can be considered.

+1
source

I personally prefer to use Marshal mainly because I avoid unsafe code. As for what happens faster, I'm not sure, but I'm sure that working pixel by pixel may be slow, but you do it. It is much better to read an entire scan line into a C # array and work on it.

0
source

Source: https://habr.com/ru/post/893678/


All Articles