简体   繁体   中英

How to change the size of the image and change the size of the rectangle that was drawn inside the picture box on a specific part of the image in C #

How to change the size of the image and the size of the rectangle that was drawn inside the image so that the rectangle does not lose the position of the x and the y. I did the following code, but it doesn't keep the position of the rectangle

int index3 = dataGridView1.CurrentCell.RowIndex;

        xxx = Convert.ToInt32(dataGridView1.Rows[index3].Cells[10].Value.ToString());
        yyy = Convert.ToInt32(dataGridView1.Rows[index3].Cells[11].Value.ToString());
        hhh = Convert.ToInt32(dataGridView1.Rows[index3].Cells[12].Value.ToString());
        www = Convert.ToInt32(dataGridView1.Rows[index3].Cells[13].Value.ToString());
        Rectangle r = new Rectangle(xxx, yyy, www, hhh);
        
        

        int newX = (int)(xxx+ (xxx*0.2));
        int newY = (int)(yyy +( yyy*0.2));

        int newWidth = (int)(r.Width +(r.Width*0.2));
        int newHeight = (int)(r.Height +(r.Height*0.2));

       
        picturModule.Size = new Size((int)(picturModule.Width+(picturModule.Width*0.2)),(int)(picturModule.Height+(picturModule.Height*0.2)));
         r = new Rectangle(newX, newY, newWidth, newHeight);
        
        Pen pen = new Pen(Color.GreenYellow, 4);
        picturModule.CreateGraphics().DrawRectangle(pen, r);

The problem is when changing the size of the image after drawing the rectangle on the image, the rectangle is not moved to the correct X and Y point and its position changes. I want when changing the size of the image that the size of the rectangle changes and keeps the same point from which you started

Okay. What you can do is store the position and size of the Rectangle as a "percentage" of the width/height of the image/picturebox. This could be done using RectangleF and floating point values.

Now you can use those "percentage" values and multiply them by the new picturebox width/height to get the scaled location and size.

Here's an example:

在此处输入图像描述

Code that generated the animation:

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    private bool drawRect = false;
    private RectangleF rcF;

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (drawRect)
        {
            Point pt = new Point((int)(rcF.X * pictureBox1.Width),
                (int)(rcF.Y * pictureBox1.Height));
            Size sz = new Size((int)(rcF.Width * pictureBox1.Width),
                (int)(rcF.Height * pictureBox1.Height));
            e.Graphics.DrawRectangle(Pens.Black, new Rectangle(pt, sz));
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Rectangle initialRectangle = new Rectangle(new Point(300, 150), new Size(125, 50));
        PointF ptF = new PointF((float)initialRectangle.X / pictureBox1.Width,
            (float)initialRectangle.Y / pictureBox1.Height);
        SizeF szF = new SizeF((float)initialRectangle.Width / pictureBox1.Width,
            (float)initialRectangle.Height / pictureBox1.Height);
        rcF = new RectangleF(ptF, szF);
        drawRect = true;
        pictureBox1.Invalidate();
    }

    private void pictureBox1_SizeChanged(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM