Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
Graphics g = Graphics.FromImage(pictureBox1.Image)
g.DrawString(txt, fnt, myBrush, pnt.X, pnt.Y )

DrawString adds a caption to the original 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. -
VB
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 white space around the image

What I have tried:

I have tested Both methods above
Posted
Updated 3-Feb-23 13:37pm
v3

1 solution

It could be due to the transformation.

Here is a simple solution to do what you want.
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        pictureBox1.MaximumSize = pictureBox1.Size;

        LoadBitmap();
    }

    private void LoadBitmap()
    {
        bitmap = new Bitmap("test image.jpg");
        pictureBox1.Image = bitmap;
    }

    private Bitmap bitmap;

    public Bitmap RotateImage(Bitmap bmp)
    {
        bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);

        // return a copy of the rotated bitmap
        return new Bitmap(bmp);
    }

    private void OnRotateClick(object sender, EventArgs e)
    {
        bitmap = RotateImage(bitmap);
        pictureBox1.Image = bitmap;
    }

    private void OnAddTextClick(object sender, EventArgs e)
    {
        string txt = "test text";
        Font fnt = DefaultFont;
        SolidBrush myBrush = new(Color.AliceBlue);

        // place text bottom-left with padding
        PointF pnt = new(5f, bitmap.Height - fnt.Height - 5f);

        Graphics g = Graphics.FromImage(bitmap);
        
        g.DrawString(txt, fnt, myBrush, pnt.X, pnt.Y);

        pictureBox1.Image = bitmap;
    }
}

NOTES:
* I am keeping a base reference to the loaded image and working with that. The PictureBox is given a reference to it.
* After I rotate the image, I return a copy, removing any transformation information. If I don't the text won't print at 90-degree & 270-degree angles.
 
Share this answer
 
Comments
Ralf Meier 4-Feb-23 12:00pm    
+5 for that ...
Graeme_Grant 4-Feb-23 14:10pm    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900