how to rotate picturebox image

Robert 0 Reputation points
2023-02-03T20:13:43.13+00:00

I am developing an application to display a digital pictures and allow text captions to be added to the picture. Often the picture needs to be rotated. I am using Visual Studio and C# and picturebox. I have tried two methods to rotate an the image and there are problems with each method that I have not been able to resolve. Below are the two methods I have tried and the problem.

Any help will be appreciated, thank you.

Method 1. - bm.RotateFlip

Bitmap bm = new Bitmap(pictureBox1.Image);
pictureBox1.Image = Rotate_Image( bm , (float)90.0);
bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Image = bm;
pictureBox1.Refresh();

Problem 1. - I am using DrawString to add the caption text

Graphics g = Graphics.FromImage(pictureBox1.Image)
g.DrawString(txt, fnt, myBrush, pnt.X, pnt.Y )

DrawString adds a caption to the origional image. Once the image is rotated, a caption can not be added in the bottom 1/3 of the image. The DrawString method works for the top 2/3 part of the image but once below a certain point. Need help

Method 2.

Bitmap rotatedBmp = new Bitmap(image.Height, image.Width);
Graphics g = Graphics.FromImage(rotatedBmp);  //make a graphics object from the empty bitmap
g.TranslateTransform(image.Height/2, image.Width/2);  //Put the rotation point in the center of the image
g.ScaleTransform((float)1.0, (float)1.0);
g.RotateTransform( (float) 90.0);  //rotate the image
g.TranslateTransform( -xx, -yy);  //move the image back
g.DrawImage(image, new PointF(0, 0));  //draw passed in image onto graphics object            

Problem 2. - Clipped image

once the image is rotated the image is clipped and there is white space on each side of the image( I can use "DrawString" to add a caption to the whit space around the image,

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
{count} votes