If you are here because of the name, you probably just skip the question and go to the answers. Turns out I had a simple error in my code.
I try to work exclusively with indexed images, because the bulk of my project involves sharing palettes. I tried the following few lines of code as part of a larger process:
Bitmap raw = ((Bitmap)i).Clone(region, PixelFormat.Format8bppIndexed);
byte transparent = (byte)(Array.FindIndex(raw.Palette.Entries, x => x.A < 128));
BitmapData bd = raw.LockBits(region, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
Basically, I wanted to crop the specified regioninput, and then perform manipulations at the lower level on this part of the image.
The listing succeeds (and I can verify that it is an indexed bitmap at this point, but it is stored in the control, hence the loss of type information). It seems that the clone call succeeds. Debugging shows that PixelFormatof is rawvalid PixelFormat.Format8bppIndexedas well as meaning (Bitmap)i. Calculating the index of a transparent palette works fine. But then it raw.LockBitsfails, complaining of an invalid parameter.
Even a stranger raw.LockBitswill succeed if I delete the call .Clone(but the rest of the code will do the wrong thing because the source has not been trimmed).
Per , , Clone - , , LockBits - . ? Graphics.DrawImage , , .
RGB, ? - ?