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