Method 1:
private void Rotateformcenter (PictureBox pb, float angle) { Image img = PB. Image; int newwidth = Math.max (img. Height, IMG. Width); Bitmap bmp = New Bitmap (newwidth, newwidth); Graphics g = graphics.fromimage (BMP); Matrix x = new Matrix (); PointF point = New PointF (img. WIDTH/2F, IMG. HEIGHT/2F); X.rotateat (angle, point); G.transform = x; G.drawimage (IMG, 0, 0); G.dispose (); img = BMP; Pb. Image = img; }
This method rotates the picture by converting it to a 3x3 matrix for geometric transformations.
Cons: Sometimes the picture gets more blurred.
Method 2:
Private voidRotateformcenter (PictureBox Pb,floatangle) {Graphics Graphics=PB. CreateGraphics (); Graphics. Clear (PB. BackColor); //Loading PicturesBitmap image =NewBitmap (pb. Image); //gets the center point of the current windowRectangle rect =NewRectangle (0,0, PB. Width, PB. Height); PointF Center=NewPointF (rect. Width/2, Rect. Height/2); floatOffsetX =0; floatOffsetY =0; OffsetX= Center. X-image. Width/2; OffsetY= Center. Y-image. Height/2; //to construct a picture display area: Align the center point of the picture with the center point of the windowRectangleF Picrect =NewRectangleF (OffsetX, OffsetY, image. Width, image. Height); PointF Pcenter=NewPointF (picrect.x + picrect.width/2, Picrect.y+ Picrect.height/2); //the drawing plane rotates at the center point of the pictureGraphics. TranslateTransform (pcenter.x, PCENTER.Y); Graphics. RotateTransform (angle); //resumes panning of the drawing plane in both horizontal and vertical directionsGraphics. TranslateTransform (-pcenter.x,-pcenter.y); //Drawing PicturesGraphics. DrawImage (image, Picrect);}
By manipulating the graphics to rotate the image, it is important to note that the picture is the SizeMode property of the center rotation PictureBox by the original size is not valid for this method.
WinForm a method of rotating a picture in a PictureBox at any angle