/// <summary> /// 轉換成Image並旋轉 /// </summary> /// <param name="file"></param> /// <returns></returns> private Image <Bgr, byte> FileToImageRotate(HttpPostedFileBase file) { using (MemoryStream ms = new MemoryStream()) { var theImage = Image.FromStream(file.InputStream); Bitmap bitmap = new Bitmap(theImage); MemoryStream stream = new MemoryStream(); foreach (var item in theImage.PropertyItems) { bitmap.SetPropertyItem(item); } var exif = new EXIFextractor(ref bitmap, "n"); RotateFlipType flip = _webModel.GetOrientationToFlipType(exif["Orientation"] == null ? "0" : exif["Orientation"].ToString()); if (flip != RotateFlipType.Rotate180FlipNone) { foreach (var item in theImage.PropertyItems) { bitmap.SetPropertyItem(item); } } bitmap.RotateFlip(flip); //先不做回存tag動作(有BUG) //exif.setTag(0x112, "1"); // Optional: reset orientation tag bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); stream.Position = 0; byte[] image = new byte[stream.Length + 1]; stream.Read(image, 0, image.Length); var faceImage = new Image <Bgr, byte>(new Bitmap(stream)); return(faceImage); } }