Image Rotation Function

Source: Internet
Author: User
# Region image rotation Function
/// <Summary>
/// Rotate the image counterclockwise
/// </Summary>
/// <Param name = "B"> bitmap stream </param>
/// <Param name = "angle"> Rotation Angle [0,360] (provided by the front end) </param>
/// <Returns> </returns>
Public bitmap rotate (Bitmap B, int angle)
{
Angle = angle % 360;

// Radians Conversion
Double radian = angle * Math. PI/180.0;
Double cos = math. Cos (radian );
Double sin = math. Sin (radian );

// width and height of the source image
int W = B. width;
int H = B. height;
int W = (INT) (math. max (math. ABS (w * Cos-H * sin), math. ABS (w * Cos + H * sin);
int H = (INT) (math. max (math. ABS (w * sin-H * Cos), math. ABS (w * SIN + H * Cos);

// Target bitmap
Bitmap dsimage = new Bitmap (W, H );
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (dsimage );

G. interpolationmode = system. Drawing. drawing2d. interpolationmode. bilinear;

G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;

// Calculate the offset
Point offset = new point (w-w)/2, (h-h)/2 );

// Construct the image display area: make the center of the image consistent with the center of the window
Rectangle rect = new rectangle (offset. X, offset. Y, W, H );
Point center = new point (rect. x + rect. width/2, rect. Y + rect. Height/2 );

G. translatetransform (center. X, Center. y );
G. rotatetransform (360-angle );

// Restore the horizontal and vertical translation of the image
G. translatetransform (-center. X,-center. y );
G. drawimage (B, rect );

// Reproduce all transformations of the Drawing
G. resettransform ();

G. Save ();
G. Dispose ();
// Dsimage. Save ("yuancd.jpg", system. Drawing. imaging. imageformat. JPEG );
Return dsimage;
}
# Endregion image rotation Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.