Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell me is it possible to rotate rectangle control to certain degrees in windows form...? Like i want to rotate the rectangle to around 45 degrees, so can i achieve this...?

Thanks in advance.


The code is as follows:


C#
private void Form1_Load(object sender, EventArgs e)
 {
 Image img = pictureBox1.Image;       
 RotateImage(img, 45);
 }

     public static Image RotateImage(Image img, float rotationAngle)
        {
            //create an empty Bitmap image
            Bitmap bmp = new Bitmap(img.Width, img.Height);

            //turn the Bitmap into a Graphics object
            Graphics gfx = Graphics.FromImage(img);

            //gfx.Clear(Color.Transparent);

           
            //now we set the rotation point to the center of our image
            gfx.TranslateTransform((float)img.Width / 2, (float)img.Height / 2);

            //now rotate the image
            gfx.RotateTransform(rotationAngle);

            gfx.TranslateTransform(-(float)img.Width / 2, -(float)img.Height / 2);

            //set the InterpolationMode to HighQualityBicubic so to ensure a high
            //quality image once it is transformed to the specified size
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //gfx.Clear(Color.White);
            //now draw our new image onto the graphics object
            gfx.DrawImage(img, new Point(0, 0));

            //dispose of our Graphics object
            gfx.Dispose();

            //return the image
            return img;
        
    }
Posted
Updated 6-Oct-13 21:26pm
v3

1 solution

 
Share this answer
 
Comments
Jagadisha_Ingenious 7-Oct-13 2:31am    
@ArunRajendra: As per the solution i will be able to rotate the image not the control, but i need to rotate the control itself. In windows form we cannot add image directly to the form(background image can be changed) but i have a image of 25pix which is added using either picturebox or a imagelist. Also if we try to rotate the image in the picture box it rotates & background we find original image without orientation. Any solution for this....?
ArunRajendra 7-Oct-13 2:39am    
Is it possible to post code?
Jagadisha_Ingenious 7-Oct-13 3:27am    
@ArunRajendra : I have added the code, do let me know the correction.

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