Jak użyć unsigned int do szybkiego dostępu do bitmapy?

0
public static void Array1DFromBitmap(Bitmap bmp)
        {
            if (bmp == null) throw new NullReferenceException("Bitmap is null");
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            IntPtr ptr = data.Scan0;
            int w = bmp.Width;
            int h = bmp.Height;
            int numInts = w * h;
            Int32[] ints = new Int32[numInts];
            System.Runtime.InteropServices.Marshal.Copy(ptr, ints, 0, numInts);
            ptr = data.Scan0;
            for (int i = 0; i < numInts; i++) ints[i] = 0x7f202020;//<---tutaj alpha jest max 0x7f
            System.Runtime.InteropServices.Marshal.Copy(ints, 0, ptr, numInts);
            bmp.UnlockBits(data);
        }

nie daje się cast z uint na int gdy >0xf7

0

Nie daje się, bo przekraczasz zakres. Castuj na long np.

0

Ale potem ten long i tak będę musiał wpisać do tablicy int. Składam z bajtów, z R,G,B nie ma problemu ale jest z Alpha.Nawet gdy chcę tam na stałe wpisać 0xFF i będzie ujemny, to muszę jakoś zmienić R,G,B.
Cały problem że Scan0 jest IntPtr a nie UIntPtr choć to drugie byłoby lepsze. I nie można zmienić typu.
Mogę zrobić Marshal.Copy na Byte[] a nie UInt[]

0

Lepiej działać na tablicy bajtów ten sposób może być:

public static void Array1DFromBitmap(Bitmap bmp)
        {
            if (bmp == null) throw new NullReferenceException("Bitmap is null");
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            IntPtr ptr = data.Scan0;
            int w = bmp.Width;
            int h = bmp.Height;
            int numBytes = data.Stride * bmp.Height;
            byte[] bytes = new byte[numBytes];            
            System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, numBytes);
            ptr = data.Scan0;
            for (int i = 0; i < numBytes; i += 4)
            {
                bytes[i+2] = 0xFF;//Red
                bytes[i + 3] = 0xFF;//Alpha
            }
            System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, numBytes);
            bmp.UnlockBits(data);
        }

1 użytkowników online, w tym zalogowanych: 0, gości: 1