dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
Shine S
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » Graphics

How to rotate an image of picturebox with desired angle in C#?


Posted Date:     Category: Graphics    
Author: Member Level: Gold    Points: 15


Sometimes it is necessory to rotate an image using code. Creating new bitmap and putting it back to picturebox is very easy. This sample code is used for the same. I hope it will be useful for you all.



 


How to rotate an image of picturebox with desired angle in C#?
Introduction:
In windows form , put one picturebox and add some image into it.
Put one button and give the name as btnRotate to it. Put one textbox to get the desired angle from user.
After clicking on btnRotate, image which is present in picturebox1 should rotate with the desired angle given by user.
btnRotate_Click code is as follows:

private void btnRotate_Click(object sender, EventArgs e)
{
Bitmap resultPicture = MoveImageByDesiredAngle(pictureBox1.Image.Width, pictureBox1.Image.Height, Convert.ToInt32(textBox1.Text));
pictureBox1.Image = resultPicture;
}


Method MoveImageByDesiredAngle is used to rotate an image with desired angle. code is as follows:


private Bitmap MoveImageByDesiredAngle(int original_width , int original_height, int desiredAngle)
{
Bitmap resultPicture = new Bitmap(original_width, original_height);
Graphics g = Graphics.FromImage(resultPicture);
g.TranslateTransform((float)original_width / 2, (float)original_height / 2);
g.RotateTransform(desiredAngle);
g.TranslateTransform(-(float)original_width / 2, -(float)original_height / 2);
g.DrawImage(pictureBox1.Image , new Point(0, 0));
return resultPicture;
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to rotate an image of picturebox with desired angle in C#?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Create beautiful List using CSS
    Previous Resource: How to crerate and move a bus in C
    Return to Resources
    Post New Resource
    Category: Graphics


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.