什么是从位图获取像素列的最快方法? 我不考虑两个循环,因为它可能需要很长时间。任何想法我都会很感激。谢谢!什么是获取像素的位图列的最佳和快速的方法?
回答
我会建议您的位图转换成使用下面的代码的字节数组:
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
。
假设你只需要一个列,以下例程应该可以帮到你。
首先这里是标准的解决方案采用可验证的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#编译器
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;
}
这两个函数将返回unsafe
支持一个Color数组,其中包含位图中特定列的像素的颜色。
将其转换为字节数组
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;
}
为什么我必须使用跨度?如果我想在图像上制作两个循环,其长度不会是100 .. – deadfish
当我得到BitmapData时,我使用了PixelFormat.Format24bppRgb集,因此每个像素使用24位(每个像素使用8位,绿色和蓝色组件),但是行的大小可能不是像素大小的精确倍数,因为为了提高效率,系统将其打包在从四字节边界开始的行中,并且步幅是单行像素,四舍五入到四个字节的边界 – MishaU
我在我的代码中发现错误:(我必须使用bitmapData.Stride而不是bitmap.Width * 3当我得到像素:result [k,i,j] = bitmapBytes [j * bitmapData.Stride + i * 3 + k]; – MishaU
- 1. 从audiofile获取图像的最佳方法是什么?
- 2. 什么是定位丢失图像的最佳方法
- 3. 获取集合元素的最快方法是什么?
- 4. 什么是从闪存中的快速图像加载系列中删除闪烁的最佳方法?
- 5. 什么是最快的方式获取地图的子图
- 6. 用于Oracle for .NET的最佳和快速的ORM是什么?
- 7. 什么是读取JavaFX Canvas像素的最佳方式?
- 8. 获得位置(x,y)的最快SVG元素的方法是什么?
- 9. 编辑这些1像素背景图像的最佳方法是什么?
- 10. 获得快速且准确的位置的最佳方式
- 11. 比较Python3中散列位的最快方法是什么?
- 12. 获取当前用户的UserId的最佳方法是什么?
- 13. 捕获图像后调用StartPreview()的最佳方式是什么?
- 14. 用C学习GLADE的最佳/最快方法是什么?
- 15. 在C中旋转bmp图像的最快方法是什么?
- 16. 在Windows.Forms中绘制图像的最快方法是什么?
- 17. 什么是旋转jpg图像文件的最快方法?
- 18. 获取QPixmap散列的最佳方式是什么?
- 19. 处理破损图像的最佳方法是什么?
- 20. 在HTML中裁剪图像的最佳方法是什么?
- 21. 什么是使用硒比较图像的最佳方法
- 22. 从网址获取图像和保存的最佳PHP方法?
- 23. 获取MethodBase对象的最快方法是什么?
- 24. 使用java获取HTML内容的最快方法是什么?
- 25. 什么是从网站下载所有图像的最快速和最简单的方法
- 26. 什么是在OnInit()中获取控件值的最佳方法?
- 27. ASP.NET中获取当前域的最佳方法是什么?
- 28. 从png获取svg的最佳方法是什么?
- 29. 找出图像列是否为空的最佳方法是什么?
- 30. 快速搜索大型平面文件的最佳方法是什么?
你只想得到一个像素阵列在图中单个列? –
@ChrisTaylor无论如何,但它必须是与像素的像素列,像上面:)哇,如此之快 – deadfish