简体   繁体   English

C 中如何改变图像的大小和改变在图像特定部分的图片框内绘制的矩形的大小#

[英]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.如何更改图像的大小和图像内部绘制的矩形的大小,使矩形不丢失 x 和 y 的 position。 I did the following code, but it doesn't keep the position of the rectangle我做了以下代码,但它没有保留矩形的 position

int index3 = dataGridView1.CurrentCell.RowIndex; 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.问题是在图像上绘制矩形后更改图像大小时,矩形没有移动到正确的 X 和 Y 点,它的 position 发生变化。 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.您可以做的是将矩形的 position 和大小存储为图像/图片框宽度/高度的“百分比”。 This could be done using RectangleF and floating point values.这可以使用RectangleF和浮点值来完成。

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:生成 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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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