【问题标题】:What is the best and fast way to get pixels' column of bitmap?获取像素的位图列的最佳和快速方法是什么?
【发布时间】:2011-12-03 16:26:33
【问题描述】:

从位图中获取像素列的最快方法是什么? 我不考虑两个循环,因为它可能需要很长时间。我会很感激任何想法。谢谢!

【问题讨论】:

  • 您是否只想获取位图中单列的像素数组?
  • @ChrisTaylor 无论如何,但它必须是带有像素的位图列,如上 :) 哇,这么快

标签: c# performance bitmap pixel


【解决方案1】:

假设您只需要一个列,以下例程应该可以帮助您。

这里首先是使用可验证 C# 代码的标准解决方案

static Color[] GetPixelColumn(Bitmap bmp, int x)
{
  Color[] pixelColumn = new Color[bmp.Height];
  for (int i = 0; i < bmp.Height; ++i)
  {
    pixelColumn[i] = bmp.GetPixel(x, i);
  }
  return pixelColumn;
}

这是一个更快的替代方案,但这需要您启用对 C# 编译器的 unsafe 支持

static Color[] GetPixelColumnFast(Bitmap bmp, int x)
{
  Color[] pixelColumn = new Color[bmp.Height];
  BitmapData pixelData = bmp.LockBits(
    new Rectangle(0, 0, bmp.Width, bmp.Height),
    ImageLockMode.ReadOnly,
    PixelFormat.Format32bppArgb);
  unsafe
  {
    int* pData = (int*)pixelData.Scan0.ToPointer();
    pData += x;
    for (int i = 0; i < bmp.Height; ++i)
    {
      pixelColumn[i] = Color.FromArgb(*pData);
      pData += bmp.Width;
    }
  }
  bmp.UnlockBits(pixelData);

  return pixelColumn;
}

这两个函数都将返回一个 Color 数组,其中包含位图中特定列的像素颜色。

【讨论】:

    【解决方案2】:

    我建议使用以下代码将您的位图转换为字节向量:

    Bitmpa myImage=new Bitmap("myImage.bmp");    
    
    BitmapData bmpData1=myImage.LockBits(new Rectangle(0,0,myImage.Width,myImage.Height), 
                     System.Drawing.Imaging.ImageLockMode.ReadWrite, myImage.PixelFormat);
    byte[] myImageData = new byte[bmpData1.Stride * bmpData1.Height];
    System.Runtime.InteropServices.Marshal.Copy(bmpData1.Scan0,myImageData,0
                           ,myImageData.Length);
    myImgage.UnlockBits(bmpData1);
    

    然后很容易检索您想要的列上的像素。只需循环遍历向量myImageData,步长等于每行的像素数 * 每像素的字节数。

    请注意用于表示位图的每个像素的字节数。 这取决于PixelFormat。

    【讨论】:

    • 当我列出所有像素的值时,是否可以更改它们并保存到新的位图?我研究了你的代码,我知道 marshal.copy 将值保存到字节数组,对吗?
    • 我发现这是可能的,只需更改字节数组中的元素,然后再次使用 marschall 副本。检查library ms
    • 小注:第一行“Bitmpa”应该是“Bitmap”
    【解决方案3】:

    将其转换为字节数组

        private static byte[, ,] BitmapToBytes(Bitmap bitmap)
        {
            BitmapData bitmapData = 
                bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            byte[] bitmapBytes;
            var stride = bitmapData.Stride;
            try
            {
                int byteCount = bitmapData.Stride * bitmap.Height;
                bitmapBytes = new byte[byteCount];
                Marshal.Copy(bitmapData.Scan0, bitmapBytes, 0, byteCount);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);                
            }
            byte[, ,] result = new byte[3, bitmap.Width, bitmap.Height];
            for (int k = 0; k < 3; k++)
            {
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        result[k, i, j] = bitmapBytes[j * stride + i * 3 + k];
                    }
                }
            }
            return result;
        }
    

    【讨论】:

    • 为什么我必须使用步幅?如果我想在 10x10 的图像上做两个循环,长度的结果不会是 100..
    • 当我得到 BitmapData 时,我使用 set PixelFormat.Format24bppRgb,所以每个像素使用 24 位(8 位每个用于红色、绿色和蓝色分量)但是行的大小可能不是像素大小的精确倍数,因为为了提高效率,系统将其打包成从四字节边界开始的行,而 Stride 是单行像素的宽度,四舍五入到四字节边界
    • 我在代码中发现错误:(,当我得到像素时,我必须使用 bitmapData.Stride 而不是 bitmap.Width * 3:result[k, i, j] = bitmapBytes[j * bitmapData.步幅 + i * 3 + k];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2018-06-07
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多