h0ppel
h0ppel

Reputation: 347

Lockbits with ushort greyscale values

I want to create a bitmap from given 16 bit greyscale values. So far I have this code:

var value = CamData.ToArray();

        var b = new Bitmap(160, 112, PixelFormat.Format24bppRgb);
        var bdata = b.LockBits(new Rectangle(0, 0, 160, 112), ImageLockMode.WriteOnly, b.PixelFormat);

        unsafe
        {
            fixed (ushort* pData = &value[0])
            {
                Marshal.Copy((IntPtr)pData, new IntPtr[]{ bdata.Scan0}, 0, value.Length);
            }
        }
        b.UnlockBits(bdata);

but I get an error in the Marshal.Copy Methode: "The requested range is beyond the end of the array". Where is the error?

thanks

Upvotes: 1

Views: 552

Answers (2)

driis
driis

Reputation: 164291

bdata.Scan0 is an IntPtr that points to the beginning of the locked memory area. You shouldn't wrap it in an array. And you can use Marshal.Copy with an array as the source. So your code could be:

Marshal.Copy(value, 0, bdata.Scan0, value.Length);

This will use this overload of Marshal.Copy.

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

You cannot copy to a memory area defined by a pointer: you need to pass real array not a pointer to an array. You are passing an array of size 1 IntPtr and that will not work.

Upvotes: 1

Related Questions