Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

c# - Get all pixel information of an image efficiently

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.

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?

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);
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at this:

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();

Is just an example, read some good tutorials about LockBits and imaging in general to understand clearly what happens.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...