【问题标题】:Drawing rectangle disappears after marking area to crop标记要裁剪的区域后绘图矩形消失
【发布时间】:2023-04-10 02:00:01
【问题描述】:

目标:

在松开鼠标左键时绘制一个红色矩形。保留矩形。

代码:

private void button1_Click(object sender, EventArgs e)
{
    if (Clipboard.ContainsImage())
    {
        pictureBox1.Image?.Dispose();
        pictureBox1.Image = Clipboard.GetImage();
    }
    else
    {
        MessageBox.Show("No Image detected in clipboard." + Environment.NewLine + "Have you print screened the label?", "Print Screen", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
        LocationXY = e.Location;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        LocationX1Y1 = e.Location;
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        LocationX1Y1 = e.Location;

        pictureBox1.Invalidate();
        pictureBox2.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
    {
        e.Graphics.DrawRectangle(Pens.Red, GetRect());
    }
}

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    var src = GetRect();

    if (src == Rectangle.Empty) return;

    var des = new Rectangle(0, 0, src.Width, src.Height);

    e.Graphics.DrawImage(pictureBox1.Image,
        des, src, GraphicsUnit.Pixel);
}


private Rectangle GetRect()
{
    return new Rectangle(
        Math.Min(LocationXY.X, LocationX1Y1.X),
        Math.Min(LocationXY.Y, LocationX1Y1.Y),
        Math.Abs(LocationXY.X - LocationX1Y1.X),
        Math.Abs(LocationXY.Y - LocationX1Y1.Y)
        );
}

private Bitmap GetCroppedImage()
{
    var des = GetRect();

    if (des == Rectangle.Empty) return null;

    var b = new Bitmap(des.Width, des.Height);

    using (var g = Graphics.FromImage(b))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, des.Width, des.Height), des, GraphicsUnit.Pixel);
    }
    return b;
}

结果:

点击一个按钮,这个打印的屏幕图像就会显示出来:

[![在此处输入图片描述][1]][1]

如果我使用鼠标左键单击并拖动,会出现这个红色矩形:

[![在此处输入图片描述][2]][2]

我一发布:

[![在此处输入图片描述][1]][1]

问题:

如何标记要裁剪的区域并将红色矩形保留在那里?没有消失

【问题讨论】:

    标签: c#


    【解决方案1】:

    因为你只在按下鼠标左键时才绘制红色矩形,

    它不是由释放鼠标左键后随后的pictureBox1_Paint 调用绘制的。

    通过以下小改动,我们可以绘制红色矩形并使其持久化:

    // Draw the red rectangle not only when
    // the left mouse button is pressed
    // but only when GetRect() is not empty
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle currentSelection = GetRect();
        if (currentSelection != Rectangle.Empty)
        {
            e.Graphics.DrawRectangle(Pens.Red, currentSelection);
        }
    }
    

    注意:如果 pictureBox1.Image 为空,pictureBox2_Paint 会失败。

    【讨论】:

    • 虽然对我有用;让我看看有没有其他变化。
    • 我再次复制了您的完整源代码,重现了该问题,并且仅更改了此方法(就像我的回答中一样),并且有效。
    • 你能分享一下(它的最终版本)吗?是的,也许在您的问题或投递箱等中。
    • 是的!它正在工作! :D 我意识到我做错了什么。如何在按钮单击时删除?
    • LocationXY = LocationX1Y1 = new Point(0, 0); 应该会自动删除它。 (空矩形)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2015-10-18
    • 2012-10-13
    • 2012-05-16
    • 2012-01-05
    • 2020-08-27
    • 2013-02-26
    相关资源
    最近更新 更多