public Bitmap Rotate1BppIndexedImage(Bitmap SourceBitmap)
{
int originalWidth = SourceBitmap.Width;
int originalHeight = SourceBitmap.Height;
Bitmap rotatedBitmap = new Bitmap(SourceBitmap.Height, SourceBitmap.Width, SourceBitmap.PixelFormat);
BitmapData rotatedData = rotatedBitmap.LockBits(new Rectangle(0, 0, rotatedBitmap.Width, rotatedBitmap.Height), ImageLockMode.WriteOnly, SourceBitmap.PixelFormat);
BitmapData SourceBitmapData = SourceBitmap.LockBits(new Rectangle(0, 0, originalWidth, originalHeight), ImageLockMode.ReadOnly, SourceBitmap.PixelFormat);
int newWidthMinusOne = rotatedBitmap.Width-1;
int newWidth = rotatedBitmap.Width;
unsafe
{
for (int y=0; y< originalHeight; y++)
{
int destinationX = newWidthMinusOne - y;
for(int x =0; x<originalWidth; x++)
{
bool Pixel;
Pixel = GetIndexedPixel(x, y, SourceBitmapData);
int DestinationPixel_X = x + y * originalWidth;
int DestinationPixel_Y = destinationX + x * newWidth;
SetIndexedPixel(DestinationPixel_X, DestinationPixel_Y, rotatedData, Pixel);
}
}
SourceBitmap.UnlockBits(SourceBitmapData);
rotatedBitmap.UnlockBits(rotatedData);
rotatedBitmap.SetResolution(SourceBitmap.HorizontalResolution, SourceBitmap.VerticalResolution);
}
return rotatedBitmap;
}
private unsafe void SetIndexedPixel(int x, int y, BitmapData bmd, bool pixel)
{
byte* p = (byte*)bmd.Scan0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));
if (pixel)
p[index] |= mask;
else
p[index] &= (byte)(mask ^ 0xff);
}
protected unsafe bool GetIndexedPixel(int x, int y, BitmapData bmd)
{
byte* p = (byte*)bmd.Scan0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));
return ((p[index] & mask)!=0);
}`
This code is working find for 32Bpprgb but not working for 1bppIndexed file. I am trying to rotate 1Bpp image but not working. It says AccessViolation @ this line => rotatedPointer[destinationPosition] = originalPointer[sourcePosition]; Thanks in advance for help. My image Size is 18144 X 46800 1Bpp indexed Bitmap image.