1

画像ボックス内の画像を回転させたい。これが私のコードです。

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);

次に、グループボックスに画像を割り当てる必要がある状況で使用されるのと同じステートメントであるため、画像がポストの直後に表示されないのはなぜですか。ここで機能しないのはなぜですか?以前に検索したことがありますが、ほとんどの検索は 90,180,270 を回転するフィリップ関数を使用しているため、私には関係がないようです。しかし、最大10度まである程度回転させたいです。

4

2 に答える 2

0

まあwin Forms、それは変換や回転を意図したものではないことを知りました。モードをに変更AutoSizeしても違いはありません。回転と変形に最適なのはWPF.
WPFオブジェクトに影響を与えずにオブジェクトを回転および変換する優れた変換クラスがあります。オブジェクトはぼやけません。This
を 使用して、回転と変換を行うことができます。

于 2013-08-27T10:17:16.013 に答える