2
Answers

draw an a square on image in picturebox

hi,

i need help on to draw square on image when fire mouse click, basically like click down and mark the red dot with size of 10px x 10px,

the image would be in the picturebox. after draw the points, i need the position of the points.
here is some code that i try but missing in somewhere,

hopefully someone could help me on

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RedDotAnnotate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Bitmap OriginalImage;
Point myPts = Point.Empty;

private void button1_Click(object sender, EventArgs e)
{
//Open File Dialog to load image files
openFileDialog1.FileName = "";
openFileDialog1.Title = "Images";

//Filter the filedialog, so that it will show only the mentioned format images
openFileDialog1.Filter = "PNG Image(*.png)|*.png|JPG Image(*.jpg)|*.jpg|BMP Image(*.bmp)|*.bmp";
openFileDialog1.ShowDialog();

if (openFileDialog1.FileName.ToString() != "")
{
pictureBox1.ImageLocation = openFileDialog1.FileName.ToString();
OriginalImage = new Bitmap(openFileDialog1.FileName.ToString());
}

// butSave.Enabled = true;
}

private void pictureBox1_mouseDown(object sender, MouseEventArgs e)
{
Point myPts = new Point(e.X, e.Y);
Bitmap b = (Bitmap)pictureBox1.Image;
Graphics g = Graphics.FromImage(b);
// myPts.add(new Point(e.X, e.Y));
Invalidate();

}
// List<Point> myPts = new List<Point>();

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{

Graphics g = e.Graphics;
foreach (Point p in myPts)
{
g.DrawEllipse(new Pen(Color.Green), p.X, p.Y, 10, 10);

}

}


}

}

Answers (2)