【问题标题】:boundaries cut the image when rotated旋转时边界会剪切图像
【发布时间】:2013-08-26 11:20:42
【问题描述】:

我想旋转图片框中的图像。这是我的代码。

public static Bitmap RotateImage(Image image, PointF offset, float angle)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            var rotatedBmp = new Bitmap(image.Width, image.Height);
            rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            var g = Graphics.FromImage(rotatedBmp);

            g.TranslateTransform(offset.X, offset.Y);

            g.RotateTransform(angle);

            g.TranslateTransform(-offset.X, -offset.Y);

            g.DrawImage(image, new PointF(0, 0));

            return rotatedBmp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Image image = new Bitmap(pictureBox1.Image);
            pictureBox1.Image = (Bitmap)image.Clone();
            var oldImage = pictureBox1.Image;
            var p = new Point(image.Width / 2, image.Height);
            pictureBox1.Image = null;
            pictureBox1.Image = RotateImage(image, p, 1);
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            pictureBox1.Refresh();
            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Image image = new Bitmap(pictureBox1.Image);
            pictureBox1.Image = (Bitmap)image.Clone();
            var oldImage = pictureBox1.Image;
            var p = new Point(image.Width / 2, image.Height);
            pictureBox1.Image = null;
            pictureBox1.Image = RotateImage(image, p, -1);
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            pictureBox1.Refresh();
            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }

现在的问题是,当我旋转图像时,它会被剪切。这是情况。

我已经拉伸了图片框并改变了表格的颜色只是为了清晰的图片。 我的问题是我何时使用了该语句

 pictureBox1.Image = RotateImage(image, p, 1);

那么为什么图片在发布后没有正确显示,因为这与我们必须将某些图像分配给 groupbox 的任何情况下使用的语句相同。为什么它在这里不起作用?我以前搜索过它,但大多数搜索似乎与我无关,因为它们使用旋转通过 90,180,270 的 filip 函数。但我想旋转某个度数,最大可达 10 度。

【问题讨论】:

    标签: c# rotation system.drawing


    【解决方案1】:

    默认情况下不支持旋转Controls(相关链接:link1link2)。图片被剪掉的原因是,旋转后,它的宽度比pictureBox1大;因此,一个快速的解决方案是在旋转后更新其大小:

    pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; //Adapts the size automatically
    

    pictureBox1.Width = image.Width;
    pictureBox1.Height = image.Height;
    

    这应该是一个可接受的解决方案(无论如何,必须有足够的可用空间来考虑图像在旋转后的新尺寸)。另一种选择是直接影响PictureBox 控件(例如,通过影响定义其边界的矩形)这将更加困难。

    【讨论】:

      【解决方案2】:

      我知道win Forms 不适合任何变换和旋转。将模式更改为AutoSize 并没有什么不同。最好的旋转和变换是WPF
      WPF 有一个很好的变换类,可以在不影响物体的情况下旋转和变换物体。对象不会变得模糊。
      您可以使用This 进行旋转和变换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 2017-10-09
        • 2013-05-18
        • 1970-01-01
        相关资源
        最近更新 更多