I'm just as confused as xtknight. I don't see how you can optimize the code as much as you say without making it unsafe. Anyway, I have rewritten the crop function with pointers (I presume this makes it unsafe) to see if this is what you mean. I'm using argb because it makes it easier just to read in an integer. I've also written some similar code (but longer) for the autocrop function, but I'm not including that here.
Code:
private void cropImage(ref Bitmap b)
{
BitmapData image = b.LockBits(new Rectangle(0, 0, b.width, b.height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
int* pointer = image.Scan0.ToPointer();
int* pixel;
int stride = image.Stride;
int white = Color.white.ToArgb()
int lineGap = stride - (4*b.width);
int centerJump = b.width - left - right;
pixel = pointer;
for (int j = 0; j < top; j++) {
for (int i = 0; i < b.Width; i++) {
*pixel = white;
pixel++;
}
pixel += lineGap;
}
for (int j = top; j < b.Height - bottom; j++)
{
for (int i = 0; i < left; i++) {
*pixel = white;
pixel++;
}
pixel += centerJump;
for (int i = 0; i < right; i++) {
*pixel = white;
pixel++;
}
pixel += lineGap;
}
for (int j = b.Height-bottom; j < b.Height; j++)
{
for (int i = 0; i < b.Width; i++)
{
*pixel = white;
pixel++;
}
pixel += lineGap;
}
b.UnlockBits(image);
}