简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM