I have this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace mws
{
public partial class PaddingPoints : Form
{
private Point point;
private Rectangle _mrect;
private List<PointF> points = new List<PointF>();
private Point RectStartPoint;
private Image img;
private Image imgClone;
private Pen myPen;
private int n = 37;
private Bitmap _bmpBU = null;
public PaddingPoints()
{
InitializeComponent();
this.DoubleBuffered = true;
_bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");
myPen = new Pen(Brushes.Red, 2);
img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g;
using (g = Graphics.FromImage(img))
{
g.DrawImage(_bmpBU, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
imgClone = (Bitmap)img.Clone();
pictureBox1.Image = img;
}
private void PaddingPoints_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label12.Text = string.Format("X = {0} Y = {1}", e.Location.X, e.Location.Y);
if (e.Button == MouseButtons.Left && e.Location != RectStartPoint)
{
label2.Text = points.Count.ToString();
label6.Text = points.Count.ToString();
label7.Text = points.Count.ToString();
label8.Text = points.Count.ToString();
DrawRectangle(e.Location);
}
}
private void DrawRectangle(Point pnt)
{
PointF pointf = new PointF(0, 0);
points = new List<PointF>();
Graphics g = Graphics.FromImage(img);
int width, height, i, x, y;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
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 = (int)((Math.Abs(RectStartPoint.X - pnt.X)) / (n - 1));
height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y)) / (n - 1));
x = Math.Min(RectStartPoint.X, pnt.X);
y = Math.Min(RectStartPoint.Y, pnt.Y);
for (i = 0; i < n - 1; i++)
{
g.FillEllipse(Brushes.Green, new Rectangle(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, y - 3, 6, 6));
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6));
pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3);
points.Add(pointf);
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();
pictureBox1.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
label10.Text = ((n - 1) * 4).ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
ScanBmpFast((Bitmap)this.pictureBox1.Image, true, _mrect);
this.pictureBox1.Refresh();
}
}
private unsafe void ScanBmpFast(Bitmap bmp, bool setToOpaqueYellow, Rectangle rect)
{
BitmapData b1 = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
int stride = b1.Stride;
int nWidth = bmp.Width;
int nHeight = bmp.Height;
System.IntPtr Scan0 = b1.Scan0;
byte* p = (byte*)(void*)Scan0;
int sX = Math.Min(Math.Max(rect.X, 0), bmp.Width);
int sY = Math.Min(Math.Max(rect.Y, 0), bmp.Height);
int w = Math.Min(rect.Width, bmp.Width - sX);
int h = Math.Min(rect.Height, bmp.Height - sY);
for (int y = sY; y < sY + h; y++)
{
p = (byte*)(void*)Scan0;
p += (y * stride + sX * 4);
for (int x = sX; x < sX + w; x++)
{
if (p[0] != 0 || p[1] != 0 || p[2] != 0)
{
p[1] = p[2] = (byte)255;
if (setToOpaqueYellow)
{
p[0] = (byte)0;
p[3] = (byte)255;
}
}
p += 4;
}
}
bmp.UnlockBits(b1);
}
}
}
Inside the method DrawRectangle i'm drawing a rectangle in red in real time when i'm moving the mouse.
Now i want to add a code to the DrawRectangle method that while it's drawing the rectangle it will also draw/add a line from the left edge middle to the right edge middle. And another line from the top edge middle to the bottom edge middle.
Tried some ways but it didn't work.
I couldn't find how to calculate the distance between the edges left and right, up and down and how to draw a line between each two edges.
I don't mind to make that it will draw this lines in the mouse up event or only if i click on a button. And also how to make it in the DrawRectangle method in real time.
Can someone show me please what should i change/add to this code ?
The DrawRectangle method i didn't do it but i'm using it for other things so i need to keep it but to add to it what i need in the question.