public static Bitmap ToRotatedBitmap(this Bitmap sourceBitmap, ExifOrientation orientation) { if (orientation == ExifOrientation.ORIENTATION_UNDEFINED || orientation == ExifOrientation.ORIENTATION_NORMAL) { return(sourceBitmap); } var width = sourceBitmap.Width; var height = sourceBitmap.Height; try { using (var matrix = new Matrix()) { switch (orientation) { case ExifOrientation.ORIENTATION_FLIP_HORIZONTAL: matrix.PostScale(-1, 1); break; case ExifOrientation.ORIENTATION_ROTATE_180: matrix.PostRotate(180); break; case ExifOrientation.ORIENTATION_FLIP_VERTICAL: matrix.PostRotate(180); matrix.PostScale(-1, 1); break; case ExifOrientation.ORIENTATION_TRANSPOSE: matrix.PostRotate(90); matrix.PostScale(-1, 1); break; case ExifOrientation.ORIENTATION_ROTATE_90: matrix.PostRotate(90); break; case ExifOrientation.ORIENTATION_TRANSVERSE: matrix.PostRotate(270); matrix.PostScale(-1, 1); break; case ExifOrientation.ORIENTATION_ROTATE_270: matrix.PostRotate(270); break; } return(Bitmap.CreateBitmap(sourceBitmap, 0, 0, width, height, matrix, false)); } } finally { if (sourceBitmap != null && sourceBitmap.Handle != IntPtr.Zero && !sourceBitmap.IsRecycled) { sourceBitmap.Recycle(); sourceBitmap.TryDispose(); } } }
public static Bitmap RotateToCorrentOrientation(this Bitmap bitmap, ExifOrientation currentOrientation) { //Calculate rotation float degrees = 0; switch (currentOrientation) { case ExifOrientation.TopRight: degrees = 90; break; case ExifOrientation.BottomLeft: degrees = -90; break; case ExifOrientation.BottomRight: degrees = 180; break; } //Rotate if needed if (degrees != 0) { using (var mtx = new Matrix()) { mtx.PreRotate(degrees); bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false); } } return(bitmap); }
public static RotateFlipType GetRotateFlipType(this ExifOrientation orientation) { switch (orientation) { case ExifOrientation.HorizontalFlip: return(RotateFlipType.RotateNoneFlipX); case ExifOrientation.Rotate180: return(RotateFlipType.Rotate180FlipNone); case ExifOrientation.VerticalFlip: return(RotateFlipType.Rotate180FlipX); case ExifOrientation.Transpose: return(RotateFlipType.Rotate90FlipX); case ExifOrientation.Rotate270: return(RotateFlipType.Rotate90FlipNone); case ExifOrientation.Transverse: return(RotateFlipType.Rotate270FlipX); case ExifOrientation.Rotate90: return(RotateFlipType.Rotate270FlipNone); default: return(RotateFlipType.RotateNoneFlipNone); } }
/// <summary> /// Returning an int so I dont need to link exif into the native apps. /// </summary> /// <param name="imgBits"></param> /// <returns></returns> public static int readExifOrientation(byte[] imgBits) { ExifOrientation result = ExifOrientation.Undefined; if (imgBits != null) { using (var resource = new MemoryStream(imgBits)) { try { JpegInfo jpegInfo = ExifReader.ReadJpeg(resource); // What exif lib associates each orientation with num in spec: // ExifLib.ExifOrientation.TopRight == 6; // i need to rotate clockwise 90 // ExifLib.ExifOrientation.BottomLeft == 8; // i need to rotate ccw 90 // ExifLib.ExifOrientation.BottomRight == 3; // i need to rotate 180 // ExifLib.ExifOrientation.TopLeft ==1; // do nada. // What each image I set the exif on resulted in: // (note: what I set should be correct as it displays right in programs that adjust for exif) // Unchd: 1 // ImgRotLeft: 6 // ImgRotRight: 8 // ImgRot180: 3 // Cool. These all tie out with images in Dave Perret article. result = jpegInfo.Orientation; //int imgExifWidth = jpegInfo.Width; //int imgExifHeight = jpegInfo.Height; Debug.WriteLine("DHB:GlobalSingletonHelpers:readExifOrientation orientation:" + result); } catch (Exception e) { Debug.WriteLine("DHB:GlobalSingletonHelpers:readExifOrientation bad exif read"); Debug.WriteLine(e.ToString()); } } } return((int)result); }
public static Bitmap RotateToCorrentOrientation(this Bitmap bitmap, ExifOrientation currentOrientation) { //Calculate rotation float degrees = 0; switch (currentOrientation) { case ExifOrientation.TopRight: degrees = 90; break; case ExifOrientation.BottomLeft: degrees = -90; break; case ExifOrientation.BottomRight: degrees = 180; break; } //Rotate if needed if (degrees != 0) { using (Matrix mtx = new Matrix()) { mtx.PreRotate(degrees); bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, mtx, false); } } return bitmap; }
public static Image buildFixedRotationImageFromBytes(byte[] inImg, ExifOrientation imgExifO = ExifOrientation.Undefined, int width = -1, int height = -1) { if (inImg == null) { return(null); } Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes"); DateTime step0 = DateTime.Now; Image result = new Image(); DateTime step1 = DateTime.Now; SKBitmap rotatedBmp = buildFixedRotationSKBitmapFromBytes(inImg, imgExifO); Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes rotBmp done"); if ((width > -1) && (height > 1) && (rotatedBmp != null)) { SKImageInfo sizing = new SKImageInfo(width, height); rotatedBmp = rotatedBmp.Resize(sizing, SKBitmapResizeMethod.Hamming); } Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes resize done"); DateTime step2 = DateTime.Now; if (rotatedBmp != null) { result = SKImageToXamarinImage(SKImage.FromBitmap((SKBitmap)rotatedBmp)); } Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes resize done"); DateTime step3 = DateTime.Now; //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step1:" + (step1 - step0)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step2:" + (step2 - step1)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step3:" + (step3 - step2)); return(result); }
/// <summary> /// Examines the exif metadata in am image and determines if the picture was rotated /// when it was taken. If the orientation is rotated, the image will be rotated to make it /// straight. /// </summary> /// <param name="image"></param> private static void FixImageOrientation(Bitmap image) { try { ExifOrientation orientation = ExifOrientation.Normal; try { orientation = ExifMetadata.FromImage(image).Orientation; if (orientation == ExifOrientation.Unknown) { orientation = ExifOrientation.Normal; } } catch (Exception e) { Debug.Fail("Unexpected error getting image orientation", e.ToString()); } if (orientation == ExifOrientation.Rotate270CW) { image.RotateFlip(RotateFlipType.Rotate90FlipXY); } else if (orientation == ExifOrientation.Rotate90CW) { image.RotateFlip(RotateFlipType.Rotate270FlipXY); } } catch (Exception) { //image.PropertyItems will throw an exception if the image does not contain any exif data } }
public static SKBitmap HandleOrientation(SKBitmap bitmap, ExifOrientation exifOrientation) { SKBitmap rotated; switch (exifOrientation) { case ExifOrientation.Rotate180: using (var surface = new SKCanvas(bitmap)) { surface.RotateDegrees(180, bitmap.Width / 2, bitmap.Height / 2); surface.DrawBitmap(bitmap.Copy(), 0, 0); } return(bitmap); case ExifOrientation.MirrorHorizontal: rotated = new SKBitmap(bitmap.Height, bitmap.Width); using (var surface = new SKCanvas(rotated)) { surface.Translate(rotated.Width, 0); surface.RotateDegrees(90); surface.DrawBitmap(bitmap, 0, 0); } return(rotated); case ExifOrientation.MirrorVertical: rotated = new SKBitmap(bitmap.Height, bitmap.Width); using (var surface = new SKCanvas(rotated)) { surface.Translate(0, rotated.Height); surface.RotateDegrees(270); surface.DrawBitmap(bitmap, 0, 0); } return(rotated); case ExifOrientation.Rotate90: rotated = new SKBitmap(bitmap.Height, bitmap.Width); using (var surface = new SKCanvas(rotated)) { surface.Translate(rotated.Width, 0); surface.RotateDegrees(90); surface.DrawBitmap(bitmap, 0, 0); } return(rotated); default: return(bitmap); } }
public static Size GetOrientatedSize(Size size, ExifOrientation orientation) { switch (orientation) { case Transpose: case Rotate90: case Transverse: case Rotate270: return(new Size(size.Height, size.Width)); default: return(new Size(size.Width, size.Height)); } }
private void GetPhotoLocation(MediaFile file) { using (Stream photo = file.GetStream()) { var picture = ExifReader.ReadJpeg(photo); ExifOrientation orientation = picture.Orientation; ExifGpsLatitudeRef latRef = picture.GpsLatitudeRef; ExifGpsLongitudeRef longRef = picture.GpsLongitudeRef; latitude.Text = GetLatitude(latRef, picture.GpsLatitude).ToString(); longitude.Text = GetLongitude(picture.GpsLongitude).ToString(); } }
private void GetPhotoTaskCompleted(object sender, PhotoResult e) { if (getPhotoCallback != null) { if (e.TaskResult == TaskResult.OK) { JpegInfo info = ExifReader.ReadJpeg(e.ChosenPhoto, e.OriginalFileName); e.ChosenPhoto.Seek(0, SeekOrigin.Begin); ExifOrientation _orientation = info.Orientation; int _angle = 0; switch (info.Orientation) { case ExifOrientation.TopLeft: case ExifOrientation.Undefined: _angle = 0; break; case ExifOrientation.TopRight: _angle = 90; break; case ExifOrientation.BottomRight: _angle = 180; break; case ExifOrientation.BottomLeft: _angle = 270; break; } Stream capturedImage; if (_angle > 0d) { capturedImage = RotateStream(e.ChosenPhoto, _angle); } else { capturedImage = e.ChosenPhoto; } IMobeelizerFile file = Mobeelizer.CreateFile("photo", capturedImage); this.getPhotoCallback(file); this.getPhotoCallback = null; } else { this.GetPhoto(getPhotoCallback); } } }
private void PreviewPhoto(MediaFile mediaFile) { using (Stream photo = mediaFile.GetStream()) { var picture = ExifReader.ReadJpeg(photo); FilePath = mediaFile.Path; ExifOrientation orientation = picture.Orientation; ExifGpsLatitudeRef latRef = picture.GpsLatitudeRef; ExifGpsLongitudeRef longRef = picture.GpsLongitudeRef; Latitude = GpsHelper.GetLatitude(latRef, picture.GpsLatitude); Longitude = GpsHelper.GetLongitude(picture.GpsLongitude); } }
public static Bitmap GetScaledAndRotatedBitmap(this Bitmap bitmap, ExifOrientation currentOrientation, float maxPixelDimension) { var newBitmapSize = getScaledSize(bitmap.Width, bitmap.Height, maxPixelDimension); if (newBitmapSize != SizeF.Empty) { var scaled = bitmap.ScaleBitmap(newBitmapSize); var rotated = scaled.RotateToCorrentOrientation(currentOrientation); if (rotated != scaled) { scaled.Dispose(); } return(rotated); } return(bitmap.RotateToCorrentOrientation(currentOrientation)); }
/// <summary> /// Given an exif orientation, rotates the passed in image accordingly /// </summary> /// <param name="orientation"></param> /// <param name="image"></param> public static bool RotateFromExifOrientation(ExifOrientation orientation, Image image) { switch (orientation) { case ExifOrientation.Rotate90CW: image.RotateFlip(RotateFlipType.Rotate90FlipNone); return(true); case ExifOrientation.Rotate270CW: image.RotateFlip(RotateFlipType.Rotate270FlipNone); return(true); case ExifOrientation.Normal: case ExifOrientation.Unknown: return(false); } return(false); }
public static IProcessor[] GetTransforms(this ExifOrientation orientation) { switch (orientation) { case FlipHorizontal: return(new IProcessor[] { FlipTransform.Horizontally }); case Rotate180: return(new IProcessor[] { new RotateTransform(180) }); case FlipVertical: return(new IProcessor[] { FlipTransform.Vertically }); case Transpose: return(new IProcessor[] { FlipTransform.Horizontally, new RotateTransform(270) }); case Rotate90: return(new IProcessor[] { new RotateTransform(90) }); case Transverse: return(new IProcessor[] { FlipTransform.Horizontally, new RotateTransform(90) }); case Rotate270: return(new IProcessor[] { new RotateTransform(270) }); default: return(Array.Empty <IProcessor>()); } }
public static Bitmap GetScaledAndRotatedBitmap(this Bitmap bitmap, ExifOrientation currentOrientation, float maxPixelDimension) { var newBitmapSize = getScaledSize(bitmap.Width, bitmap.Height, maxPixelDimension); if (newBitmapSize != SizeF.Empty) { var scaled = bitmap.ScaleBitmap(newBitmapSize); var rotated = scaled.RotateToCorrentOrientation(currentOrientation); if (rotated != scaled) scaled.Dispose(); return rotated; } else { return bitmap.RotateToCorrentOrientation(currentOrientation); } }
public static IList <Image> buildTwoFixedRotationImageFromBytes(byte[] inImg, ExifOrientation imgExifO = ExifOrientation.Undefined) { IList <Image> res = new List <Image>(2); DateTime step0 = DateTime.Now; //Image result = new Image(); res.Add(new Image()); res.Add(new Image()); DateTime step1 = DateTime.Now; SKBitmap rotatedBmp = buildFixedRotationSKBitmapFromBytes(inImg, imgExifO); DateTime step2 = DateTime.Now; if (rotatedBmp != null) { //result = SKImageToXamarinImage(SKImage.FromBitmap((SKBitmap)rotatedBmp)); SKImage img = SKImage.FromBitmap((SKBitmap)rotatedBmp); res[0] = SKImageToXamarinImage(img); res[1] = SKImageToXamarinImage(img); } DateTime step3 = DateTime.Now; //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step1:" + (step1 - step0)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step2:" + (step2 - step1)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationImageFromBytes step3:" + (step3 - step2)); //return result; return(res); }
private bool OrientImage(string sourceFileName, string destinationFileName) { Image image = null; bool isProperlyDone = false; try { image = Image.FromFile(sourceFileName); ExifOrientation orientation = ImageOrientation(image); switch (orientation) { case ExifOrientation.TopLeft: break; case ExifOrientation.TopRight: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case ExifOrientation.BottomRight: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case ExifOrientation.BottomLeft: image.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case ExifOrientation.LeftTop: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case ExifOrientation.RightTop: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case ExifOrientation.RightBottom: image.RotateFlip(RotateFlipType.Rotate90FlipY); break; case ExifOrientation.LeftBottom: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // setting image orientation to top left //propOrient.Value[0] = 1; //image.SetPropertyItem(propOrient); image.Save(destinationFileName); isProperlyDone = true; } catch (Exception ex) { richTextFailed.AppendText(string.Format("{0} - {1}\r\n", sourceFileName, ex.Message)); } finally { if (image != null) { image.Dispose(); } } return(isProperlyDone); }
/// <summary> /// Given an exif orientation, rotates the passed in image accordingly /// </summary> /// <param name="orientation"></param> /// <param name="image"></param> public static bool RotateFromExifOrientation(ExifOrientation orientation, Image image) { switch (orientation) { case ExifOrientation.Rotate90CW: image.RotateFlip(RotateFlipType.Rotate90FlipNone); return true; case ExifOrientation.Rotate270CW: image.RotateFlip(RotateFlipType.Rotate270FlipNone); return true; case ExifOrientation.Normal: case ExifOrientation.Unknown: return false; } return false; }
private static string GetDisplayString(ExifOrientation tag) { return(ExifOrientationStrings.ContainsKey(tag) ? ExifOrientationStrings[tag] : tag.ToString()); }
public PageInfo(int width, int height, ExifOrientation orientation = ExifOrientation.None) { Width = width; Height = height; Orientation = orientation; }
public static SKBitmap buildFixedRotationSKBitmapFromBytes(byte[] imgBits, ExifOrientation imgExifO = ExifOrientation.Undefined) { SKBitmap rotatedBmp = null; if (imgBits != null) { DateTime step0 = DateTime.Now; using (var resource = new MemoryStream(imgBits)) { //using (var stream = new SKManagedStream(resource)) { //using (var stream = new MemoryStream((resource)) { DateTime step1 = DateTime.Now; if (imgExifO == ExifOrientation.Undefined) { imgExifO = ExifLib.ExifOrientation.TopLeft; try { JpegInfo jpegInfo = ExifReader.ReadJpeg(resource); // What exif lib associates each orientation with num in spec: // ExifLib.ExifOrientation.TopRight == 6; // i need to rotate clockwise 90 // ExifLib.ExifOrientation.BottomLeft == 8; // i need to rotate ccw 90 // ExifLib.ExifOrientation.BottomRight == 3; // i need to rotate 180 // ExifLib.ExifOrientation.TopLeft ==1; // do nada. // What each image I set the exif on resulted in: // (note: what I set should be correct as it displays right in programs that adjust for exif) // Unchd: 1 // ImgRotLeft: 6 // ImgRotRight: 8 // ImgRot180: 3 // Cool. These all tie out with images in Dave Perret article. imgExifO = jpegInfo.Orientation; //int imgExifWidth = jpegInfo.Width; //int imgExifHeight = jpegInfo.Height; //string res = "Orient:" + imgExifO.ToString() + " W:" + imgExifWidth + ", H:" + imgExifHeight; //string res2 = res + "dummy"; } catch (Exception e) { Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationSKBitmapFromBytes bad exif read"); Debug.WriteLine(e.ToString()); } } DateTime step2 = DateTime.Now; try { SKBitmap baseBmp = SKBitmapFromBytes(imgBits); //SKBitmap rotatedBmp = null; if (imgExifO == ExifLib.ExifOrientation.TopRight) { rotatedBmp = new SKBitmap(baseBmp.Height, baseBmp.Width); using (var canvas = new SKCanvas(rotatedBmp)) { canvas.Translate(rotatedBmp.Width, 0); canvas.RotateDegrees(90); canvas.DrawBitmap(baseBmp, 0, 0); } } else if (imgExifO == ExifLib.ExifOrientation.BottomLeft) { rotatedBmp = new SKBitmap(baseBmp.Height, baseBmp.Width); using (var canvas = new SKCanvas(rotatedBmp)) { // currently upside down. with w, 90. // failures: -W, 270 w,-90 -W,90 0,90 0,270 // h, 270 -> soln is to think about the corner I'm told is important... //canvas.Translate(-rotatedBmp.Width, 0); canvas.Translate(0, rotatedBmp.Height); canvas.RotateDegrees(270); canvas.DrawBitmap(baseBmp, 0, 0); } } else if (imgExifO == ExifLib.ExifOrientation.BottomRight) { rotatedBmp = new SKBitmap(baseBmp.Width, baseBmp.Height); using (var canvas = new SKCanvas(rotatedBmp)) { canvas.Translate(rotatedBmp.Width, rotatedBmp.Height); canvas.RotateDegrees(180); canvas.DrawBitmap(baseBmp, 0, 0); } } else { rotatedBmp = baseBmp; } Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationSKBitmapFromBytes success"); } catch (Exception e) { string msg = e.ToString(); } DateTime step3 = DateTime.Now; //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationSKBitmapFromBytes step1:" + (step1 - step0)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationSKBitmapFromBytes step2:" + (step2 - step1)); //Debug.WriteLine("DHB:GlobalSingletonHelpers:buildFixedRotationSKBitmapFromBytes step3:" + (step3 - step2)); } } return(rotatedBmp); }
public OrientTransform(ExifOrientation orientation) { Orientation = orientation; }