Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从我的锁中获取每种颜色的像素_C#_Graphics_Pixels_Imaging_Lockbits - Fatal编程技术网

C# 从我的锁中获取每种颜色的像素

C# 从我的锁中获取每种颜色的像素,c#,graphics,pixels,imaging,lockbits,C#,Graphics,Pixels,Imaging,Lockbits,我有一个程序,它获取图像中的每个像素,并将它们输出到文本文档中。问题是,它只是把它们作为数字扔进去,看起来相当难看。 e、 i.“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 Rect

我有一个程序,它获取图像中的每个像素,并将它们输出到文本文档中。问题是,它只是把它们作为数字扔进去,看起来相当难看。 e、 i.“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);
            }
        }
Bitmap bmp=新位图(“Space big.jpg”);
矩形bmpRec=新矩形(0,0,bmp.Width,bmp.Height)//创建用于保存图片的矩形
BitmapData bmpData=bmp.LockBits(bmpRec,ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb)//获取位图数据
IntPtr Pointer=bmpData.Scan0//设置指针
int DataBytes=Math.Abs(bmpData.Stride)*bmp.Height//获取数组大小
字节[]rgbValues=新字节[数据字节]//创建数组
副本(指针,RGB值,0,数据字节)//内存不足的副本
bmp.UnlockBits(bmpData);
StringBuilder Pix=新的StringBuilder(“”);
秒表计时器=新秒表();
pictureBox1.Image=bmp;
StringBuilder EachPixel=新StringBuilder(“”);
Timer.Start();
对于(int i=0;i
它只是附加到颜色结构的调试器可视化工具。您已经强制将位图解释为32bpp图像,因此适当的数组类型是int[]而不是byte[]。现在每个元素都是一个像素。您可以使用color.FromArgb(int)将其转换为颜色。