C# 如何在C中获得矩形内的所有像素值#
我正在开发一个程序来获取矩形内的所有像素。有一个图像控件,用户可以单击其中的一个部件。当用户单击特定位置时,将绘制一个矩形。我想得到矩形内的所有像素。我现在要画矩形了。但是我不能得到所有的像素值。请查找下面绘制矩形的代码段C# 如何在C中获得矩形内的所有像素值#,c#,C#,我正在开发一个程序来获取矩形内的所有像素。有一个图像控件,用户可以单击其中的一个部件。当用户单击特定位置时,将绘制一个矩形。我想得到矩形内的所有像素。我现在要画矩形了。但是我不能得到所有的像素值。请查找下面绘制矩形的代码段 private void panel1_Paint(object sender, PaintEventArgs e) { foreach (var rectKey in rectangles.Keys) { us
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();
}
private void panel 1_Paint(对象发送器,PaintEventArgs e)
{
foreach(矩形中的var rectKey.Keys)
{
using(var pen=new pen(rectKey))//创建用于绘制矩形的笔(using语句确保笔已被释放)
{
//绘制当前颜色的所有矩形
//请注意,我们正在使用传递到事件处理程序的图形对象。
e、 Graphics.Draw矩形(画笔,矩形[rectKey].ToArray());
}
}
}
专用无效面板1_鼠标(对象发送器,鼠标目标e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
Color c=System.Drawing.Color.GreenYellow;//获取要为其绘制矩形的颜色
//使用颜色作为字典的键添加矩形
如果(!矩形.ContainsKey(c))
{
添加(c,newlist());
}
矩形[c].Add(新矩形(e.Location.X-12,e.Location.Y-12,25,25));//将矩形添加到集合中
}
//使面板重新喷漆。
panel1.Refresh();//指向panel1_Paint()事件
矩形;
}
在这种情况下,您必须使用位图,而不是图形对象
位图有一种在某个位置获取像素的方法
Bitmap bmp = Bitmap.FromFile("");
// Get the color of a pixel within myBitmap.
Color pixelColor = bmp.GetPixel(50, 50);
要读取矩形内的所有像素,可以使用bmp.LockBits
方法 提议的链接已断开