当PictureBox处于“缩放”模式时,裁剪图像的正确部分

时间:2019-05-29 11:54:31

标签: c#

我有一个PictureBox1,它的sizemode设置为Stretch和PictureBox1。 PictureBox1包含一个图像,让我们选择它的一部分,然后对其进行裁切并将裁切后的部分存储在PictureBox2中。 当sizemode设置为“拉伸”且图片未缩放时,效果很好。,但当我缩放它或将sizemode设置为zoom时,效果不佳。

working example - sizemode set to 'stretch'

我用来裁剪部分图片的代码(original source

for character in word:
    if chr(Alphabet[Alphabet.find(character)) >= "z":
        new_Alphabet = Alphabet.join(Alphabet)
    elif chr(Alphabet[Alphabet.find(character)) <= "a":
        new_Alphabet = Alphabet.join(Alphabet[:-1])

如何正确计算?如何使裁剪功能发挥作用,使用户可以放大/缩小并仍然裁剪图片的正确部分?

1 个答案:

答案 0 :(得分:-1)

您需要使用拉伸系数以及偏移量来计算点。

对于Zoom,只有一个因素,因为ImagePictureBox的纵横比始终相同,但是通常存在偏移;对于Stretch,您不需要补偿,但两个因子。

下面是一个使用两个 PictureBoxes的示例,其中两个显示了缩放版本和裁剪后的位图。它利用确定大小和偏移量的通用功能ImageArea

enter image description here

两个类级变量:

Point pDown = Point.Empty;
Rectangle rect = Rectangle.Empty;

三个鼠标事件:

private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    pDown = e.Location;
    pictureBox1.Refresh();
}

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (!e.Button.HasFlag(MouseButtons.Left)) return;

    rect = new Rectangle(pDown, new Size(e.X - pDown.X, e.Y - pDown.Y));
    using (Graphics g = pictureBox2.CreateGraphics())
    {
        pictureBox1.Refresh();
        g.DrawRectangle(Pens.Orange, rect);
    }
}

private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    Rectangle iR = ImageArea(pictureBox2);
    rect = new Rectangle(pDown.X - iR.X, pDown.Y - iR.Y, 
                         e.X - pDown.X, e.Y - pDown.Y);
    Rectangle rectSrc = Scaled(rect, pictureBox2, true);
    Rectangle rectDest = new Rectangle(Point.Empty, rectSrc.Size);

    Bitmap bmp = new Bitmap(rectDest.Width, rectDest.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawImage(pictureBox1.Image, rectDest, rectSrc, GraphicsUnit.Pixel);
    }
    pictureBox2.Image = bmp;
}

这是一个有用的函数,可针对任何sizemode返回图片框内的实际图像区域。.

Rectangle ImageArea(PictureBox pbox)
{
    Size si = pbox.Image.Size;
    Size sp = pbox.ClientSize;

    if (pbox.SizeMode == PictureBoxSizeMode.StretchImage) 
       return pbox.ClientRectangle;
    if (pbox.SizeMode == PictureBoxSizeMode.Normal ||
        pbox.SizeMode == PictureBoxSizeMode.AutoSize) 
       return new Rectangle(Point.Empty, si);
    if (pbox.SizeMode == PictureBoxSizeMode.CenterImage)
        return new Rectangle(new Point((sp.Width - si.Width) / 2,
                            (sp.Height - si.Height) / 2), si);

    //  PictureBoxSizeMode.Zoom
    float ri = 1f * si.Width / si.Height;
    float rp = 1f * sp.Width / sp.Height;
    if (rp > ri)
    {
        int width = si.Width * sp.Height / si.Height;
        int left = (sp.Width - width) / 2;
        return new Rectangle(left, 0, width, sp.Height);
    }
    else
    {
        int height = si.Height * sp.Width / si.Width;
        int top = (sp.Height - height) / 2;
        return new Rectangle(0, top, sp.Width, height);
    }
}

我们只需要偏移即可确定未缩放的矩形。我们还需要扩展它:

Rectangle Scaled(Rectangle rect, PictureBox pbox, bool scale)
{
    float factor = GetFactor(pbox);
    if (!scale) factor = 1f / factor;
    return Rectangle.Round(new RectangleF(rect.X * factor, rect.Y * factor,  
                               rect.Width * factor, rect.Height * factor));
}

为此,需要知道缩放比例,具体取决于长宽比:

float GetFactor(PictureBox pBox)
{
    if (pBox.Image == null) return 0;
    Size si = pBox.Image.Size;
    Size sp = pBox.ClientSize;
    float ri = 1f * si.Width / si.Height;
    float rp = 1f * sp.Width / sp.Height;
    float factor = 1f * pBox.Image.Width / pBox.ClientSize.Width;
    if (rp > ri) factor = 1f * pBox.Image.Height / pBox.ClientSize.Height;
    return factor;
}

如果通过将PictureBox放在AutoScrolling Panel内并更改Pbox.Size放大或缩小,此解决方案也将起作用。