C #-Picture rotation and Base64 processing

Source: Internet
Author: User

Rotating

(1) Rotate by angle

    <summary>///rotate icons by angle///</summary>//<param name= "img" ></param> p          Ublic image rotateimg (image img, float angle) {//The picture is set transparently through a PNG picture, changing the black issue of the rotated picture. int width = img.          Width; int height = img.                              Height;                     Angle Matrix Mtrx = new Matrix (); Mtrx.          Rotateat (angle, new PointF (WIDTH/2), (HEIGHT/2)), matrixorder.append);          Gets the rotated rectangle GraphicsPath path = new GraphicsPath (); Path.          AddRectangle (New RectangleF (0f, 0f, width, height)); RectangleF RCT = path.          GetBounds (MTRX); Generate the target bitmap Bitmap devimage = new Bitmap ((int) (RCT). Width), (int) (RCT.          Height));          Graphics g = graphics.fromimage (devimage);          G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;                     G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Calculate offset Point offset = new POint ((int) (RCT). Width-width)/2, (int) (RCT.          Height-height)/2);          Construct the image display area: Make the center of the image consistent with the center point of the window Rectangle rect = new Rectangle (Offset.x, OFFSET.Y, (int) width, (int) height); Point Center = new Point ((int) (rect. X + rect. WIDTH/2), (int) (rect. Y + rect.                      HEIGHT/2)); G.translatetransform (center. X, center.          Y);          G.rotatetransform (angle); Restores the image in the horizontal and vertical direction of the pan G.translatetransform (-center. X,-center.          Y);          G.drawimage (IMG, rect);          All Transformations G.resettransform () to the drawing;          G.save ();          G.dispose (); Path.          Dispose ();      return devimage;  }

  (2) press radians rotate

    <summary>//////</summary>//<param name= "B" ></param>//&L T;param name= "angle" ></param>///<returns></returns> Public image RotateImg2 (image B, Floa            T angle) {angle = angle% 360;          Radians convert Double radian = angle * math.pi/180.0;          Double cos = math.cos (radian);          Double sin = Math.sin (radian);          The width and height of the original image are 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 Image 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;     Calculates the offset point offset = new Point ((W-W)/2, (H-H)/2);          To construct an image display area: Align the center of the image with the center point 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); Restores the image in the horizontal and vertical direction of the pan G.translatetransform (-center. X,-center.          Y);          G.drawimage (b, rect);          All Transformations G.resettransform () to the drawing;          G.save ();          G.dispose ();          Dsimage.save ("Yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);      return dsimage; }

Above reference: Two kinds of image rotation methods based on C #

In addition, the encapsulated method can be used directly:

<summary>  ///rotate (using encapsulated method)///</summary>//  <param name= "path" > Picture path </param> //  <param name= "RotateFlipType" > Rotation </param>//  <returns></returns> public  BOOL Kirotate (string path, RotateFlipType rotatefliptype)  {      try      {          using (Bitmap Bitmap = new Bitmap ( Path)          {              //clockwise rotation 90 degrees Rotatefliptype.rotate90flipnone            //counterclockwise rotation 90 degrees Rotatefliptype.rotate270flipnone            //Horizontal Flip rotatefliptype.rotate180flipy            //Vertical flip rotatefliptype.rotate180flipx            bitmap. RotateFlip (RotateFlipType);              Bitmap. Save (path);          return true;      }      catch (Exception ex)      {          return false;      }  

Base64 processing

One of the most common encoding methods for transmitting 8Bit bytecode is the process of Base64 encoding from binary to character, which can be used to pass longer identity information in an HTTP environment.

    • Each three bytes of 8Bit is converted to four 6Bit bytes (3*8 = 4*6 = 24), then 6Bit is added two bits high 0, which makes up four 8Bit bytes, that is, the converted string will theoretically be longer than the original 1/3
    • Characters into 4 characters, multibyte a newline character every 76 characters, and the last terminator to handle

C #-Picture rotation and Base64 processing

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.