在C#中快速使用Bitmaps

And*_*hin 38 .net c# graphics bitmap pixels

我需要访问Bitmap的每个像素,使用它们,然后将它们保存到Bitmap.

使用Bitmap.GetPixel()Bitmap.SetPixel(),我的程序运行缓慢.

我怎么能快速转换Bitmapbyte[]和回?

我需要byte[]length = (4 * width * height)含有各像素的RGBA数据.

dav*_*nal 78

你可以用几种不同的方式做到这一点.您可以使用unsafe直接访问数据,也可以使用编组来回复制数据.不安全的代码更快,但封送不需要不安全的代码.这是我做了一段时间后的表现比较.

这是使用lockbits的完整示例:

/*Note unsafe keyword*/
public unsafe Image ThresholdUA(float thresh)
{
    Bitmap b = new Bitmap(_image);//note this has several overloads, including a path to an image

    BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);

    byte bitsPerPixel = GetBitsPerPixel(bData.PixelFormat);

    /*This time we convert the IntPtr to a ptr*/
    byte* scan0 = (byte*)bData.Scan0.ToPointer();

    for (int i = 0; i < bData.Height; ++i)
    {
        for (int j = 0; j < bData.Width; ++j)
        {
            byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8;

            //data is a pointer to the first byte of the 3-byte color data
            //data[0] = blueComponent;
            //data[1] = greenComponent;
            //data[2] = redComponent;
        }
    }

    b.UnlockBits(bData);

    return b;
}
Run Code Online (Sandbox Code Playgroud)

这是同样的事情,但与编组:

/*No unsafe keyword!*/
public Image ThresholdMA(float thresh)
{
    Bitmap b = new Bitmap(_image);

    BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);

    /* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
    byte bitsPerPixel = GetBitsPerPixel(bData.PixelFormat);

    /*the size of the image in bytes */
    int size = bData.Stride * bData.Height;

    /*Allocate buffer for image*/
    byte[] data = new byte[size];

    /*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
    System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, data, 0, size);

    for (int i = 0; i < size; i += bitsPerPixel / 8 )
    {
        double magnitude = 1/3d*(data[i] +data[i + 1] +data[i + 2]);

        //data[i] is the first of 3 bytes of color

    }

    /* This override copies the data back into the location specified */
    System.Runtime.InteropServices.Marshal.Copy(data, 0, bData.Scan0, data.Length);

    b.UnlockBits(bData);

    return b;
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该知道,在执行此操作时,颜色通道的顺序可能会有所不同,具体取决于您使用的PixelFormat.例如,对于24位比特映射,像素的第一个字节是蓝色通道,然后是绿色,然后是红色,而不是通常预期的红 - 绿 - 蓝顺序. (9认同)
  • 您还应该知道,这可能无法获得预期的字节数据,因为通常`bData.Stride`不必等于`b.Width*bytePerPix`. (4认同)
  • 这里的“GetBitsPerPixel()”是什么? (3认同)
  • 谢谢,我甚至不需要任何人.:) (2认同)
  • @Parhs 当然,跳过`bData.Height` 会加快速度,因为您只获取数据的第一行。试试 `size = 1` 这会让事情变得更快! (2认同)

Sha*_*med 7

如果您使用 C# 8.0,我建议使用新版本Span<T>以获得更高的效率。

这是一个粗略的实现

public unsafe class FastBitmap : IDisposable
{
    private Bitmap _bmp;
    private ImageLockMode _lockmode;
    private int _pixelLength;

    private Rectangle _rect;
    private BitmapData _data;
    private byte* _bufferPtr;

    public int Width { get => _bmp.Width; }
    public int Height { get => _bmp.Height; }
    public PixelFormat PixelFormat { get => _bmp.PixelFormat; }

    public FastBitmap(Bitmap bmp, ImageLockMode lockMode)
    {
        _bmp = bmp;
        _lockmode = lockMode;

        _pixelLength = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
        _rect = new Rectangle(0, 0, Width, Height);
        _data = bmp.LockBits(_rect, lockMode, PixelFormat);
        _bufferPtr = (byte*)_data.Scan0.ToPointer();
    }

    public Span<byte> this[int x, int y]
    {
        get
        {
            var pixel = _bufferPtr + y * _data.Stride + x * _pixelLength;
            return new Span<byte>(pixel, _pixelLength);
        }
        set
        {
            value.CopyTo(this[x, y]);
        }
    }

    public void Dispose()
    {
        _bmp.UnlockBits(_data);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不是 `var Pixel = _bufferPtr + y * _data.Stride * x * _pixelLength;` 应该是 `var Pixel = _bufferPtr + y * _data.Stride` + `x * _pixelLength` (4认同)

小智 6

您可以使用 Bitmap.LockBits 方法。此外,如果您想使用并行任务执行,您可以使用 System.Threading.Tasks 命名空间中的 Parallel 类。以下链接有一些示例和解释。