0

I have 3 lines of code in C#, which I don't understand. I want to know the equivalent in Java/Android:

// Get the address of the first line.

// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp_sized_in.Height;
byte[] rgbValues = new byte[bytes];

// I DON'T UNDERSTAND WHAT THESE 3 LINES DO
// WHAT IS THE TYPE OF IntPtr and WHAT DOES Scan0 DO?
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

bmpData is of type BitMapData

Can somebody explain these 3 lines to me, so that I can translate them to Java for Android?

mrd
  • 4,561
  • 10
  • 54
  • 92
  • 1
    The type of IntPtr is IntPtr: https://stackoverflow.com/questions/1148177/just-what-is-an-intptr-exactly maybe this is useful as well: https://stackoverflow.com/q/17030264/578411 – rene May 15 '19 at 12:54
  • 1
    You use this code when performing operations on the content of a Bitmap , using a block of bytes locked in memory. This: `IntPtr ptr = bmpData.Scan0;` it's the pointer to the first scanline. The first `Mashal.Copy()` copies an array of bytes, starting from the address of the first scanline, to the buffer dimensioned as the content of the Bitmap bytes (`bmpData.Stride * bmp_sized_in.Height`). The second `Marshal.Copy()`, copies back the modified array to the bitmap bytes locked in memory. This is the final step after you have modified a Bitmap's bytes (applied a filter, set Colors etc.). – Jimi May 15 '19 at 13:18
  • The first operation (which is missing here) is to lock a Bitmap in memory. See [Bitmap.LockBits](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits). Which needs to be unlocked in the end: [Bitmap.UnlockBits](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.unlockbits). This method is the commonly used when you need to alter the content of a Bitmap's bytes. For example, when a filter is applied. Or you need to change a color range to another. Any calculation that spans a large number of color components. – Jimi May 15 '19 at 13:31

0 Answers0