# 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