开发者

Why some pictures are are crooked aftes using my function?

开发者 https://www.devze.com 2023-02-04 03:07 出处:网络
struct BitmapDataAccessor { private readonly byte[] data; private readonly int[] rowStarts; 开发者_运维技巧public readonly int Height;
struct BitmapDataAccessor
{
    private readonly byte[] data;
    private readonly int[] rowStarts;
  开发者_运维技巧  public readonly int Height;
    public readonly int Width;

    public BitmapDataAccessor(byte[] data, int width, int height)
    {
        this.data = data;
        this.Height = height;
        this.Width = width;
        rowStarts = new int[height];
        for (int y = 0; y < Height; y++)
            rowStarts[y] = y * width;
    }

    public byte this[int x, int y, int color] // Maybe use an enum with Red = 0, Green = 1, and Blue = 2 members?
    {
        get { return data[(rowStarts[y] + x) * 3 + color]; }
        set { data[(rowStarts[y] + x) * 3 + color] = value; }
    }

    public byte[] Data
    {
        get { return data; }
    }
}

    public static byte[, ,] Bitmap2Byte(Bitmap obraz)
    {
        int h = obraz.Height;
        int w = obraz.Width;

        byte[, ,] wynik = new byte[w, h, 3];

        BitmapData bd = obraz.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

        int bytes  = Math.Abs(bd.Stride) * h;
        byte[] rgbValues = new byte[bytes];
        IntPtr ptr = bd.Scan0;
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        BitmapDataAccessor bda = new BitmapDataAccessor(rgbValues, w, h);

        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                wynik[j, i, 0] = bda[j, i, 2];
                wynik[j, i, 1] = bda[j, i, 1];
                wynik[j, i, 2] = bda[j, i, 0];
            }
        }

        obraz.UnlockBits(bd);
        return wynik;
    }

    public static Bitmap Byte2Bitmap(byte[, ,] tablica)
    {
        if (tablica.GetLength(2) != 3)
        {
            throw new NieprawidlowyWymiarTablicyException();
        }

        int w = tablica.GetLength(0);
        int h = tablica.GetLength(1);

        Bitmap obraz = new Bitmap(w, h, PixelFormat.Format24bppRgb);

        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                Color kol = Color.FromArgb(tablica[i, j, 0], tablica[i, j, 1], tablica[i, j, 2]);
                obraz.SetPixel(i, j, kol);
            }
        }

        return obraz;
    }

Now, if I do:

    private void btnLoad_Click(object sender, EventArgs e)
    {
        if (dgOpenFile.ShowDialog() == DialogResult.OK)
        {
            try
            {
                Bitmap img = new Bitmap(dgOpenFile.FileName);
        
                byte[, ,] tab = Grafika.Bitmap2Byte(img);

                picture.Image = Grafika.Byte2Bitmap(tab);
                picture.Size = img.Size;
     

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

Most of pictures are handled correctly butsome not. Example of picture that doesn't work:

Why some pictures are are crooked aftes using my function?

(source: ifotos.pl)

It produce following result (this is only fragment of picture) :

Why some pictures are are crooked aftes using my function?

(source: ifotos.pl)

Why is that?


You need to account for BitmapData.Stride when you access the data.

Why some pictures are are crooked aftes using my function?

EDIT:

Here is a solution that I use to copy a DirectX surface to a Bitmap. The idea is the same, but you'll need to modify it slightly. I copy one scanline of the image at a time with a call to RtlMoveMemory (P/Invoke to kernel32.dll)

//// Snippet

        int pitch;
        int bytesPerPixel = 4;
        Rectangle lockRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

        // Lock the bitmap
        GraphicsStream surfacedata = surface.LockRectangle(LockFlags.ReadOnly, out pitch);
        BitmapData bitmapdata = bitmap.LockBits(lockRectangle, ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

        // Copy surface to bitmap
        for (int scanline = 0; scanline < bitmap.Height; ++scanline)
        {
            byte* dest = (byte*)bitmapdata.Scan0 + (scanline * bitmap.Width * bytesPerPixel);
            byte* source = (byte*)surfacedata.InternalData + (scanline * pitch);

            RtlMoveMemory(new IntPtr(dest), new IntPtr(source), (bitmap.Width * bytesPerPixel));
        }

////

EDIT #2:

Check this out: Stride/Pitch Tutorial

It is all aimed at DirectX but the concept is the same.


It seems the memory allocated for bitmaps must be aligned on a 32-bit boundary and so there is possibly padding on some of the images due to their size. As you have a 24-bit pixel here then some line widths will end on a 32-bit others will not. You need to use the following formula to work out the padding being used and then account for it:

int padding  = bd.Stride - (((w * 24) + 7) / 8);

You might want to load your byte array using GetPixel(x,y) rather than going through the whole transform to byte array before you start reading pixels.


Thanx to @Lazarus and tbridge I managed how to do this.

First we need to calculate padding in Bitmap2Byte:

int padding  = bd.Stride - (((w * 24) + 7) / 8);

and pass it to BitmapDataAccessor and modify the line

this.Width = width;

to

this.Width = width + (4-padding)%4;

That's all. Thanx guys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号