简体   繁体   English

为什么裁剪后的矩形不适合pictureBox sizemode?

[英]Why the cropped rectangle is not fitting the pictureBox sizemode?

In my program i did that on the listBox i have items when i select an item i see a screenshot image in the picturebox on the right. 在我的程序中,我在listBox上执行了该操作,当我选择一个项目时,我在右侧的图片框中看到一个屏幕截图图像。 Then i can draw a rectangle on the pictureBox and with a button click i can crop the rectangle. 然后,我可以在pictureBox上绘制一个矩形,然后单击按钮可以裁剪该矩形。 The problem is that i need to make the pictureBox in the form1 designer SizeMode proprty to be zoom or stretch image if not when i select an item i see the screenshot in pictureBox too large. 问题是,当我选择一个项目时,如果我没有看到pictureBox中的屏幕截图太大,我需要使form1设计器的SizeMode属性中的pictureBox成为缩放或拉伸图像。 Only if it's zoom or stretch i see it good. 只有缩放或拉伸,我才能看到它很好。 But then when i crop the rectangle it's not fitting and i need the pictureBox sizemode to be Normal. 但是然后当我裁剪矩形时,它不合适了,我需要pictureBox sizemode为Normal。 How can i fix it ? 我该如何解决?

This is my form1 code: 这是我的form1代码:

Form1.cs Form1.cs

This is how i select and item and display the image of it: 这就是我选择和显示项目的方式:

private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
        {

            WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
            selectedIndex = this.listBoxSnap.SelectedIndex.ToString();
            this.pictureBoxSnap.Image = snap.Image;
            for (int i = 0; i < rectangles.Length; i++)
            {
                if (rectangles[i] != RectClone)
                {
                    ClearGraphics = false;
                }
                else
                {
                    ClearGraphics = true;
                }
            }

        }

And this is the paint event: 这是绘画事件:

private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
        {

            if (pictureBoxSnap.Image != null)
            {
                {
                    if (ClearGraphics == false)
                    {

                            if (rectangles[listBoxSnap.SelectedIndex] != Rectangle.Empty)
                            {
                                e.Graphics.DrawRectangle(Pens.Firebrick, rectangles[listBoxSnap.SelectedIndex]);
                            }




                    }
                    if (cropRect == true)
                    {
                        if (recttest.Width > 10 && recttest.Height > 10)
                        {
                            e.Graphics.Clear(Color.White);

                            e.Graphics.DrawImage(pictureBoxSnap.Image, new Rectangle(rect.X, rect.Y, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);

                        }
                    }
                }
            }           
        }

The drawimage line draw the cropped rectangle. drawimage线绘制裁剪的矩形。

The problem is that if SizeMode set to Normal i see good the cropped rectangle but then the screenshot/image when i select an item dosent fit the pictureBox too large. 问题是,如果将SizeMode设置为Normal,我会看到裁剪好的矩形,但是当我选择一个项目时,屏幕快照/图像适合pictureBox太大。 And if i change the sizemode to zoom i see the selected item image good but the cropped image is not fitting. 如果我将sizemode更改为zoom,我会看到所选项目的图像很好,但是裁剪后的图像不合适。

EDITED: 编辑:

When it set to Normal i see the cropped image in the rectangle good but then the screenshot of a selected item look like zoom in very big/wide in the pictureBox. 当将其设置为“正常”时,我会在矩形中看到裁剪好的图像,但是所选项目的屏幕截图看起来像是在pictureBox中放大/放大。 And if it set to zoom i see the image of a item good but then the rectangle i draw i see the cropped image inside not what i marked with the rectangle but more like the image 0,0 area part. 如果将其设置为缩放,则我会看到一个项目的图像很好,但是我绘制的矩形却不是在我用矩形标记的区域内看到的裁剪图像,而更像是图像0,0区域部分。

This is a link with 4 images i took and explained each one what SizeMode the pictureBox is and what is the problem. 这是我拍摄的4张图像的链接,并分别解释了pictureBox的SizeMode和问题所在。 I can't upload here images so i did an album in my facebook 我无法在此处上传图片,所以我在Facebook上做了相册

....
....
if (cropRect == true){
    if (recttest.Width > 10 && recttest.Height > 10){
        e.Graphics.FillRectangle(Brushes.White, 0, 0, pictureBoxSnap.Width, rect.Y);
        e.Graphics.FillRectangle(Brushes.White, 0, rect.Y, rect.X, rect.Height);
        e.Graphics.FillRectangle(Brushes.White, rect.X + rect.Width, rect.Y, pictureBoxSnap.Width - rect.X - rect.Width, rect.Height);
        e.Graphics.FillRectangle(Brushes.White, 0, rect.Y + rect.Height, pictureBoxSnap.Width, pictureBoxSnap.Height - rect.Y - rect.Height);
     }
}    

or better 或更好

private void pictureBoxSnap_Paint(object sender, PaintEventArgs e){
    Point pnt;

    ....
    ....
    if (cropRect == true){
        if (recttest.Width > 10 && recttest.Height > 10){
            pnt = PointToScreen(pictureBoxSnap.Location);

            e.Graphics.Clear(Color.White);
            e.Graphics.CopyFromScreen(pnt.X + rect.X, pnt.Y + rect.Y, rect.X, rect.Y, New Size(rect.Width, rect.Height));
        }
    }

valter 瓦尔特

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

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