Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i am creating a software, which loads image(fingerprint) in picture box after i want to draw circles on the picturebox on the place i have clicked mouse. I created events on pictureBox1 MouseDown(for clicking) and Paint(for drawing). When i load my form and click on pictureBox1 nothing happens, no circles are drawn, BUT after i load image in pictureBox1 all the circles that should have been drawn before appear on picturebox with loaded image. If i click on picturebox after loading image nothing gets drawn and only if re-load image new circles appear. What am i doing wrong?

Thanks for answer

here is my code and screenshot:

Screenshot

namespace fingerprint_test_v04
{
    public partial class Form1 : Form
    {
        private ArrayList myPts = new ArrayList();
        private Bitmap img = null;

        public Form1()
        {
            InitializeComponent();
            //CenterToScreen();
            this.Text = "FingerprintzzzZZzzZZzzz";

        }

        public void MyPaintHandler(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
        }


        private void openFPR2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pictureBox2.Image = img;
                    img = new Bitmap(open.FileName);
                }
            }
            catch (Exception)
            {
                throw new ApplicationException("Failed loading image");
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = img;
                    img = new Bitmap(open.FileName);
                    angle.Enabled = true;
                    angle_ValueChanged(null, EventArgs.Empty);
                }
            }
            catch (Exception)
            {
                throw new ApplicationException("Failed loading image");
            }
        }

        private void angle_ValueChanged(object sender, System.EventArgs e)
        {
            if (angle.Value > 359.9m)
            {
                angle.Value = 0;
                return;
            }

            if (angle.Value < 0.0m)
            {
                angle.Value = 359;
                return;
            }

            Image oldImage = pictureBox1.Image;
            pictureBox1.Image = Program.RotateImage(img, (float)angle.Value);

            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
            this.Controls.Add(pictureBox1);
            
        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.Handle);
            myPts.Add(new Point(e.X, e.Y));
            Invalidate(); 
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach (Point p in myPts)
                g.DrawEllipse(new Pen(Color.Red, 3), p.X, p.Y, 10, 10);
        }

     }
}
Posted
Updated 16-Jan-10 5:50am
v2

1 solution

Refresh the picture box after you paint:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    foreach (Point p in myPts)
        g.DrawEllipse(new Pen(Color.Red, 3), p.X, p.Y, 10, 10);
    (sender as PictureBox).Refresh();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900