0

I tried to translate this code to vb.net using Marshal.copy but I can't get it to work

for (int y = 0; y < bitmapdata.Height; y++)
{
    byte* destPixels = (byte*)bitmapdata.Scan0 + (y * bitmapdata.Stride);
    for (int x = 0; x < bitmapdata.Width; x++)
    {
        destPixels[x * PixelSize] = contrast_lookup[destPixels[x * PixelSize]]; // B
        destPixels[x * PixelSize + 1] = contrast_lookup[destPixels[x * PixelSize + 1]]; // G
        destPixels[x * PixelSize + 2] = contrast_lookup[destPixels[x * PixelSize + 2]]; // R
        //destPixels[x * PixelSize + 3] = contrast_lookup[destPixels[x * PixelSize + 3]]; //A
    }
}

My problem is this line:

byte* destPixels = (byte*)bitmapdata.Scan0 + (y * bitmapdata.Stride);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
camaya
  • 377
  • 1
  • 4
  • 15
  • Similar question, http://stackoverflow.com/questions/13174323/contrast-of-an-image-in-vb2005/ – Kratz Nov 03 '12 at 23:28

1 Answers1

2

Assuming that you have Scan0 as an IntPtr then the naive translation to C# is simply:

IntPtr destPixels = Scan0 + y*stride;

The players here are:

  • Scan0: a pointer to the first scanline, i.e. the beginning of the pixel data.
  • y: the row number.
  • stride: the number of bytes in a row of pixels.
  • destPixels: a pointer to the beginning of row y.

But this would be under the assumption that you were using unmanaged memory for destPixels. I don't know whether or not you are. If you are using managed memory, then the translation would differ. If you want more help you need to tell us about the types that your managed version uses.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490