从我的LockBits中获取每个像素颜色

时间:2014-05-06 10:36:47

标签: c# graphics pixels imaging lockbits

我有一个程序可以获取图像中的每个像素并将它们输出到文本文档中。问题是,它只是将它们抛在一边,而且看起来很难看。 E.I. “37 37 37 36”我希望像GetPixel函数一样显示它们。 E.I. [A = 255,R = 4,G = 255,B = 131]。我目前的代码是......

Bitmap bmp = new Bitmap("Space big.jpg");

Rectangle bmpRec = new Rectangle(0, 0, bmp.Width, bmp.Height); //Creates Rectangle for holding picture

BitmapData bmpData = bmp.LockBits(bmpRec, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); //Gets the Bitmap data

IntPtr Pointer = bmpData.Scan0; //Sets pointer

int DataBytes = Math.Abs(bmpData.Stride) * bmp.Height; //Gets array size

byte[] rgbValues = new byte[DataBytes]; //Creates array

Marshal.Copy(Pointer, rgbValues, 0, DataBytes); //Copies of out memory

bmp.UnlockBits(bmpData);

StringBuilder Pix = new StringBuilder(" ");

Stopwatch Timer = new Stopwatch();

pictureBox1.Image = bmp;

        StringBuilder EachPixel = new StringBuilder("");

        Timer.Start();
        for (int i = 0; i < bmpData.Width; i++)
        {
            for (int j = 0; j < bmpData.Height; j++)
            {
                // compute the proper offset into the array for these co-ords
                var pixel = rgbValues[i + j * Math.Abs(bmpData.Stride)];
                Pix.Append(" ");
                Pix.Append(pixel);
            }
        }

0 个答案:

没有答案
相关问题