有效地获取图像的所有像素信息 [英] Get all pixel information of an image efficiently
问题描述
我制作了一个程序来从图片中获取所有图像像素RGB颜色代码。基本上,它将y位置设置为常数,并通过循环将x位置的宽度从零更改为y。
I made a program to get all image pixel RGB color codes from picture. Basically, it sets y position on constant and changes x position zero to width and also y by looping.
可以,但是问题是要花20多个分钟才能从偶数(1000 * 604高度宽度)的图像中获取所有像素。请有人帮忙吗?
Ok it's work, but the problem is it take more than 20 minutes to get all pixel from even (1000*604 height width) image. Please anyone help?
如果这个过程花了这么长时间,我感到很惊讶,那么我们如何才能从图像制作类似条形码阅读器的程序。我想从图像中获取所有像素值,这是下面的C#代码。
I'm surprised if this process takes so long, then how can we make a program like bar-code reader from image. I want to get all pixel value from image, here is my C# code are below.
我还上传了程序此处,如果您不同意,请进行检查。
I also uploaded my program here, check it if you don't agree.
void myimage() {
mypic = new Bitmap(pathname);
int imwid = mypic.Width;
int imhei = mypic.Height;
int total=imwid*imhei;
for (int z = 0; z <imhei;z++ )
{
for (int i = 0; i < imwid; i++)
{
Color pixelColor = mypic.GetPixel(i, z);
textBox2.AppendText(" " + pixelColor.R +
" " + pixelColor.G +
" " + pixelColor.B + " " +
pixelColor.A +
Environment.NewLine);
}
}
}
推荐答案
看看这个:
var data = mypic.LockBits(
new Rectangle(Point.Empty, mypic.Size),
ImageLockMode.ReadWrite, mypic.PixelFormat);
var pixelSize = data.PixelFormat == PixelFormat.Format32bppArgb ? 4 : 3; // only works with 32 or 24 pixel-size bitmap!
var padding = data.Stride - (data.Width * pixelSize);
var bytes = new byte[data.Height * data.Stride];
// copy the bytes from bitmap to array
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
var index = 0;
var builder = new StringBuilder();
for (var y = 0; y < data.Height; y++)
{
for (var x = 0; x < data.Width; x++)
{
Color pixelColor = Color.FromArgb(
pixelSize == 3 ? 255 : bytes[index + 3], // A component if present
bytes[index + 2], // R component
bytes[index + 1], // G component
bytes[index] // B component
);
builder
.Append(" ")
.Append(pixelColor.R)
.Append(" ")
.Append(pixelColor.G)
.Append(" ")
.Append(pixelColor.B)
.Append(" ")
.Append(pixelColor.A)
.AppendLine();
index += pixelSize;
}
index += padding;
}
// copy back the bytes from array to the bitmap
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
textBox2.Text = builder.ToString();
仅是示例,请阅读有关 LockBits $ c的一些不错的教程$ c>和一般成像以清楚地了解会发生什么。
Is just an example, read some good tutorials about LockBits
and imaging in general to understand clearly what happens.
这篇关于有效地获取图像的所有像素信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!