倾斜位图,RGB565 C#的步幅计算 [英] Slanted bitmap, stride calculation for RGB565 C#

查看:185
本文介绍了倾斜位图,RGB565 C#的步幅计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些结果图像是倾斜的,有些则不是。

Some of my resulting images are slanted, some are not.

预期结果:(529x22)

Expected Result: (529x22)

实际结果:(529x22)

Actual Result: (529x22)


不介意不同的图像尺寸,这些都是截图。它们都是529x22。

Don't mind the different image sizes, these are screenshots. They are both 529x22.

我正在使用的代码,我刚从SO的问题答案得到了这个。

The code I am using, I just got this from an answer on a question here at SO.

// some other method
byte[] pixels = new byte[size - 16];
Array.Copy(this.data, offset, pixels, 0, pixels.Length);
this.ByteToImage(w, h, pixels);

// builds the pixels to a image
private Bitmap ByteToImage(int w, int h, byte[] pixels)
{
    var bmp = new Bitmap(w, h, PixelFormat.Format16bppRgb565);

    var BoundsRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData = bmp.LockBits(BoundsRect,
                                    ImageLockMode.WriteOnly,
                                    bmp.PixelFormat);

    // bytes => not using this because it gives error
    // eg. pixel.Length = 16032, bytes = 16064
    int bytes = bmpData.Stride * bmp.Height;

    Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
    bmp.UnlockBits(bmpData);

    return bmp;
}

我很困惑因为有些工作正常,而不是倾斜。但其他人则倾斜。我错过了什么?

I'm confused because some works ok, not slanted. But others are slanted. What did I miss?

如评论和答案中所述,问题在于我如何计算步幅。我仍然对如何做到这一点感到困惑,但我试过这个:

As stated in the comments and answers, the problem is how I'm calculating stride. I'm still confused on how to do it but I tried this:

public static void RemovePadding(this Bitmap bitmap)
{
    int bytesPerPixel = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8;

    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
    var pixels = new byte[bitmapData.Width * bitmapData.Height * bytesPerPixel];

    for (int row = 0; row < bitmapData.Height; row++)
    {
        var dataBeginPointer = IntPtr.Add(bitmapData.Scan0, row * bitmapData.Stride);
        Marshal.Copy(dataBeginPointer, pixels, row * bitmapData.Width * bytesPerPixel, bitmapData.Width * bytesPerPixel);
    }

    Marshal.Copy(pixels, 0, bitmapData.Scan0, pixels.Length);
    bitmap.UnlockBits(bitmapData);
}

但结果是(更倾斜):

推荐答案

这似乎在这里工作:

private Bitmap ByteToImage(int w, int h, byte[] pixels)
{
    var bmp = new Bitmap(w, h, PixelFormat.Format16bppRgb565);
    byte bpp = 2;
    var BoundsRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData = bmp.LockBits(BoundsRect,
                                    ImageLockMode.WriteOnly,
                                    bmp.PixelFormat);
    // copy line by line:
    for (int y = 0; y < h; y++ )
        Marshal.Copy(pixels, y * w * bpp, bmpData.Scan0 + bmpData.Stride * y, w * bpp);
    bmp.UnlockBits(bmpData);

    return bmp;
}

我使用循环将每行数据放在正确的位置。数据不包括填充,但目标地址必须这样做。

I use a loop to place each row of data at the right spot. The data do not include the padding, but the target address must do so.

因此我们需要将数据访问乘以实际 width * bytePerPixel 但是目标地址 .aspx?f = 255& MSPPError = -2147217396rel =nofollow> Stride ,即扫描线的长度,填充到下一个倍数四个字节。对于 width = 300 stride = 300 width = 301 它是 stride = 304 ..

Therefore we need to multiply the data access by the actual width * bytePerPixel but the target adress by the Stride, i.e. the length of the scanline, padded to the next multiple of four bytes. For width=300 it is stride=300, for width=301 it is stride=304..

一步移动所有像素数据只能在那里工作没有填充,即当宽度是 4 的倍数时。

Moving all pixel data in one step can only work when there is no padding, i.e. when the width is a multiple of 4.

这篇关于倾斜位图,RGB565 C#的步幅计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆