我用鼠标绘制一个矩形,现在我希望在我绘制矩形的同时,它会在每个矩形边缘底部,顶部,龙骨,右边绘制点。
这就是我只绘制矩形时的样子:
我希望在我实时绘制矩形的同时,用X个点添加/填充矩形的每个边缘。例如,在每个边缘上,10个绿点与它们之间具有确切的空间。
例如,我添加了绘画中的点只是为了表明我的意思:
只有绿色点应该在矩形的红线和红线的粗线上。 绿色点应该填补。 这些点之间应该有确切的空间。
我只是在顶部绘制了一些点,但它也应该在左下角和右上角。
这就是我现在绘制正方形矩形的方法。
在form1的顶部:
private Bitmap _bmpBU = null;
public static Rectangle mRect;
在构造函数中:
this.DoubleBuffered = true;
_bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");
pictureBox1鼠标移动事件:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
pictureBox1.Invalidate();
}
}
pictureBox1鼠标按下事件:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
Image iOLd = this.pictureBox1.Image;
Bitmap bmp = (Bitmap)_bmpBU.Clone();
this.pictureBox1.Image = bmp;
if (iOLd != null)
iOLd.Dispose();
pictureBox1.Invalidate();
}
和pictureBox1绘制事件:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
}
}
我需要以某种方式在绘画事件中绘制矩形时它也会填充并将绿点添加到每个边缘。
答案 0 :(得分:1)
这只是数学。添加此功能:
private void DrawPointsOnRectangle(Graphics g, int numberOfPoints)
{
var brush = new SolidBrush(Color.DarkGreen);
const int rectanglePenWidth = 2;
//North & South
int spacing = mRect.Width / (numberOfPoints - 1);
for (int x = 0; x < numberOfPoints; x++)
{
g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7, 15, 15);
g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7 + mRect.Height, 15, 15);
}
//East & West
spacing = mRect.Height/(numberOfPoints - 1);
for (int y = 0; y < numberOfPoints; y++)
{
g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5, mRect.Y - 7 + (y * spacing), 15, 15);
g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5 + mRect.Width, mRect.Y - 7 + (y * spacing), 15, 15);
}
}
修改您的pictureBox1_Paint
函数以调用新函数:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
DrawPointsOnRectangle(e.Graphics, 5);
}
}
应该这样做!您可以将5
参数更改为您想要的每个点数。
答案 1 :(得分:0)
private Point RectStartPoint;
private Image img;
private Image imgClone;
private Pen myPen;
private n = 10; //number of points
您必须初始化这些对象,可能在:
public Form1()
{
InitializeComponent();
myPen = new Pen(Brushes.Red, 2);
//Bitmap to hold the picturebox image
img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g;
using (g = Graphics.FromImage(img))
{
g.DrawImage(imageOfPicturebox, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
//image to hold the original picturebox. We need it to clear img to the original
//picturebox image
imgClone = (Bitmap)img.Clone();
//We draw always on img and then we invalidate
pictureBox1.Image = img;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Location != RectStartPoint)
{
DrawRectangle(e.Location);
}
}
private void DrawRectangle(Point pnt)
{
Graphics g = Graphics.FromImage(img);
int width, height, i, x, y;
g.SmoothingMode = SmoothingMode.AntiAlias;
//Clear img from the rectangle we drawn previously
g.DrawImage(imgClone, 0, 0);
if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
{
g.DrawLine(myPen, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
}
else
{
g.DrawRectangle(myPen, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y),
Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y));
//width of spaces between points
width = (int)((Math.Abs(RectStartPoint.X - pnt.X)) / (n - 1));
//height of spaces between points
height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y)) / (n - 1));
//we always want the upper left x, y coordinates as a reference drawing clockwise
x = Math.Min(RectStartPoint.X, pnt.X);
y = Math.Min(RectStartPoint.Y, pnt.Y);
//Drawing the points. change the 3, 6 values for larger ones
for (i = 0; i < n - 1; i++)
{
//Up side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
//Right side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, y - 3, 6, 6));
//Bottom side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
//Left side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6));
x += width;
y += height;
}
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
}
g.Dispose();
//draw img to picturebox
pictureBox1.Invalidate();
}