WinForm a method of rotating a picture in a PictureBox at any angle

Source: Internet
Author: User

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

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.