I'm drawing a rectangle with the mouse and now i want that while i'm drawing the rectangle it will draw points on each of the rectangle edges bottom,top,keft,right.
This is how it look like when i'm drawing just the rectangle:
And i want that while i'm drawing the rectangle in real time to add/pad each edge of the rectangle with X number of points. For example on each edge 10 green points with exactly space between them.
For example i added the points in paint just to show what i mean:
Just the green points should be in the thick of the red lines of the rectangle and on the red lines. The green points should be filled. And there should be exact space between the points.
방금 상단에 몇 점을 그렸지만 왼쪽 하단과 오른쪽에도 있어야합니다.
이것이 내가 이제 정사각형을 직사각형으로 그리는 방법입니다.
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);
}
}
사각형을 그리는 동안 페인트 이벤트에서 어떻게 든 필요하며 각 가장자리에 녹색 점을 추가하고 추가합니다.
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();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다