Question

I want to rotate an image in the picture box. Here is my code.

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

Now the problem is that when I rotate the image it gets cut. Here is the situation. enter image description here

I have stretched the picture box and changed the colour of form just for clear picture. My question is when I have used the statement

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

Then why is the picture not getting right after postion as this is the same statement used for any situation where we have to assign some image to groupbox. Why is not it working here? I have searched it before but the most of the searches seem irrelevant to me because they use filip function which rotate through 90,180,270. But I want to rotate by some degree maximum upto 10 degree.

Was it helpful?

Solution 2

Well i have come to know that win Forms are not meant for any transformations and rotations.Changing the mode to AutoSize does not make a difference. The best thing for rotation and transformation is WPF.
WPF has a good transformation classes which rotate and transform objects without affecting the object. The object does not get blurred.
You can use This for rotations and transformations.

OTHER TIPS

Rotating Controls is not something supported by default (links talking about this: link1, link2). The reason why the picture gets cut is because, after the rotation, its width is bigger than the pictureBox1 one; thus a quick solution would be updating its size after the rotation:

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

or

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

This should be an acceptable solution (there has to be enough free space to account for the new dimensions of the image after being rotated anyway). The other option would be affecting the PictureBox control directly (by affecting the rectangle defining its boundaries, for example) what would be much more difficult.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top