0

我正在开发一个程序来获取矩形内的所有像素。有一个图像控件,用户可以单击其中的某个部分。当用户单击特定位置时,会绘制一个矩形。我想获得该矩形内的所有像素。我现在要画矩形。但我无法获得所有像素值。请在下面找到用于绘制矩形的代码片段。

private void panel1_Paint(object sender, PaintEventArgs e)
    {
        foreach (var rectKey in rectangles.Keys)
        {
            using (var pen = new Pen(rectKey))     //Create the pen used to draw the rectangle (using statement makes sure the pen is disposed)
            {
                //Draws all rectangles for the current color
                //Note that we're using the Graphics object that is passed into the event handler.
                e.Graphics.DrawRectangles(pen, rectangles[rectKey].ToArray()); 
            }
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Color c = System.Drawing.Color.GreenYellow ;     //Gets a color for which to draw the rectangle

            //Adds the rectangle using the color as the key for the dictionary
            if (!rectangles.ContainsKey(c))
            {
                rectangles.Add(c, new List<Rectangle>());
            }
            rectangles[c].Add(new Rectangle(e.Location.X - 12, e.Location.Y - 12, 25, 25));    //Adds the rectangle to the collection
        }
        //Make the panel repaint itself.
       panel1.Refresh();// Directs to panel1_Paint() event
       rectangles.Clear();
    }
4

1 回答 1

1

在这种情况下,您将不得不使用Bitmapnot 图形对象。

位图有一种方法来获取某个位置的像素

    Bitmap bmp = Bitmap.FromFile("");

    // Get the color of a pixel within myBitmap.
    Color pixelColor = bmp.GetPixel(50, 50);

要读取矩形内的所有像素,您可以使用该bmp.LockBits方法。

于 2013-10-05T11:00:58.383 回答