Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从pictureBox裁剪绘制的矩形并将裁剪后的图像指定给pictureBox时,为什么';让我们放大?_C#_.net_Winforms - Fatal编程技术网

C# 从pictureBox裁剪绘制的矩形并将裁剪后的图像指定给pictureBox时,为什么';让我们放大?

C# 从pictureBox裁剪绘制的矩形并将裁剪后的图像指定给pictureBox时,为什么';让我们放大?,c#,.net,winforms,C#,.net,Winforms,表格1顶部 Rectangle rect; bool painting = false; 建造师 this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1鼠标向下事件 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.L

表格1顶部

Rectangle rect;
bool painting = false;
建造师

this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1鼠标向下事件

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rect = new Rectangle(e.X, e.Y, 0, 0);
                painting = true;
            }
        }
picturebox1鼠标移动事件

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rect = new Rectangle(rect.Left, rect.Top, Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left), Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
            }
            this.pictureBox1.Invalidate();
        }
picturebox1绘制事件

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (painting == true)
            {

                using (Pen pen = new Pen(Color.Red, 2))
                {
                    e.Graphics.DrawRectangle(pen, rect);
                }
            }
        }
picturebox1鼠标

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            Image tmp = pictureBox1.Image;
            pictureBox1.Image = SaveRectanglePart(pictureBox1.Image, rect);
            if (tmp != null)
                tmp.Dispose();
        }
以及SaveRectaglePart方法

public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
        {
            var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height);
            using (var graphics = Graphics.FromImage(bmp))
            {
                graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
            }

            return bmp;
        }
问题是,当我在pictureBox1上绘制一个矩形,然后当它出现鼠标上升事件时,我希望它做的是用我绘制的矩形内的图像替换pictureBox1中的当前图像

在绘制的矩形中拉伸图像,并将其指定给pictureBox1,因此现在在pictureBox1中,我将从绘制的矩形内部获得裁剪后的图像

但我在图片中看到的是放大的图像。不知道为什么

这是我绘制矩形时的屏幕截图

这是我在mouseup事件中得到的截图