Shiva Sharma
Shiva Sharma

Reputation: 1

Why can't I paint a rectangle with this code in pictureBox?

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 UTUResultWithCoordinates
{
    public partial class GetCoordinates : Form
    {
        private string sem;
        private string branch;
        private int mouseisdown = 0;
        private int recx = 0;
        private int recy = 0;
        private int mousemovingwhilepressed = 0;

        public GetCoordinates()
        {
            InitializeComponent();

        }

        public GetCoordinates(string p, string p_2)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            branch = p;
            sem = p_2;
            pictureBox1.Controls.Add(pictureBox2);
            pictureBox2.Location = new Point(0, 0);
            pictureBox2.BackColor = Color.Transparent;
            pictureBox2.Width = 1191;
            pictureBox2.Height = 842;

        }

        private void GetCoordinates_Load(object sender, EventArgs e)
        {

            pictureBox1.ImageLocation =        @"D:\DotNet\UTUResultWithCoordinates\UTUResultWithCoordinates\bin\Debug\ComputerScience6.jpg";
        }






        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            if (mouseisdown == 1 && mousemovingwhilepressed==1)
            {
            System.Drawing.Graphics graphicsObj;
            graphicsObj = this.CreateGraphics();
            Pen myPen = new Pen(System.Drawing.Color.Blue, 100);
            Rectangle myRectangle = new Rectangle(recx, recy, 20, 20);
            e.Graphics.DrawRectangle(myPen, myRectangle);
         }

    }

    private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        mouseisdown = 1;
        recx = e.X;
        recy = e.Y;
        pictureBox2.CreateGraphics();

    }

    private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
    {
        label1.Text = e.X + "," + e.Y;
        mousemovingwhilepressed = 1;
        recx = e.X;
        recy = e.Y;
        pictureBox2.CreateGraphics();
    }

    private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
    {
        mousemovingwhilepressed = 0;
        mouseisdown = 0;
        pictureBox2.CreateGraphics();
    }
}

}

I have created a pictureBox1 in which an image is displayed. Then I have created a pictureBox2 inside it so that I can paint on that image a rectangle by dragging the mouse. But nothing is happening on clicking the mouse. What is the error?

Upvotes: 0

Views: 81

Answers (1)

Emond
Emond

Reputation: 50712

Calling CreateGraphics does not trigger the painting of the PictureBox.

Use Invalidate to cause a redraw.

For a full example see: How to select an area on a PictureBox.Image with mouse in C#

Side notes:

  • Calling InitializeControl in a method other than the constructor is not a good idea.
  • when you need a boolean use a boolean, not an integer.
  • Objects that implement IDisposable (such as Pen) should be created as few times as possible and be disposed when no longer needed/used.

Upvotes: 2

Related Questions