0
  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.

1 Answers1

0

Here is your first problem: int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(originalBitmap.PixelFormat)/32; You are never using the result of that call. And it is 1 for 32BppRGB and 0 for 1Bpp (dividing 1 by 32 yields 0).

Your crash is because width/height are in pixels, while your pointer size is 4. rotatedPointer[1] would point to pixels(!) 32 to 63 of a row. You can't address a single bit that way.

You basically have to rewrite this method completely to support 1Bpp, as you need to do a lot of bit-mangling to get that working.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • thanks for answer. Its my bad. I will delete that line as i used that line for incrementing Width pointer but then i changed back to working way. – jaydeep6720 Feb 09 '22 at 04:03