Problems with photos being rotated after taking pictures on some Android or iOS phones

Source: Internet
Author: User

1, our usual mobile phone photos, uploaded to the computer, using Photoshop or other image browsing tools to open, found that the picture was turned. But the preview on Windows is positive. In fact, the reason is that some Android or iOS mobile phone after taking pictures, the picture angle information into the EXIF information. We just need to read it and do the corresponding redraw.

2, the code sent.

   classImagenormal { Public voidNormalimagedegree (stringImagePath) {            varBitmap =(Bitmap) bitmap.fromfile (ImagePath); varEXIF =bitmap.            Getexif (); if(EXIF. Orientation = = ExifNET.Models.Types.Orientation.Normal)return; Bitmap Newbitmap=NULL; Switch(EXIF. Orientation) { CaseExifNET.Models.Types.Orientation.Rotate90:newBitmap= Rotateimage (Bitmap,- -);  Break;  CaseExifNET.Models.Types.Orientation.Rotate270:newBitmap= Rotateimage (Bitmap,- the);  Break;  CaseExifNET.Models.Types.Orientation.Rotate180:newBitmap= Rotateimage (Bitmap,- the);  Break; default:                     Break; } bitmap.            Dispose (); if(Newbitmap! =NULL) {Newbitmap.save (ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);            Newbitmap.dispose (); }        }        PrivateBitmap Rotateimage (Bitmap B,floatangle) {Angle= Angle% the; //radians Conversion            DoubleRadian = angle * Math.PI/180.0; Doublecos =Math.Cos (Radian); DoubleSin =Math.sin (Radian); //the width and height of the original            intW =B.width; inth =B.height; intW = (int) (Math.max (Math.Abs (w * cos-h * sin), math.abs (w * cos + h *sin)); intH = (int) (Math.max (Math.Abs (w * sin-h * cos), math.abs (w * sin + H *cos)); //Target BitmapBitmap Dsimage =NewBitmap (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 offsetPoint Offset =NewPoint (W-W)/2, (H-H)/2); //to construct an image display area: Align the center of the image with the center of the windowRectangle rect =NewRectangle (Offset.x, Offset.y, W, h); Point Center=NewPoint (rect. X + rect. Width/2, Rect. Y + rect. Height/2); G.translatetransform (center. X, center.            Y); G.rotatetransform ( the-angle); //restores the panning of the image in both horizontal and vertical directionsG.translatetransform (-center. X,-Center.            Y);            G.drawimage (b, rect); //all transformations to the drawingG.resettransform ();            G.save ();            G.dispose (); //dsimage.save ("Yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);            returnDsimage; }    }

Problems with photos being rotated after taking pictures on some Android or iOS phones

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.