private static void RotateDimension(float width, float height, MediaFileImageOrientation originalOrientation, MediaFileImageOrientation orientationDest, out float w, out float h) { var retW = width; var retH = height; Action rotate = () => { var wT = retW; retW = retH; retH = wT; }; switch (originalOrientation) { case MediaFileImageOrientation.Left: case MediaFileImageOrientation.LeftMirrored: case MediaFileImageOrientation.Right: case MediaFileImageOrientation.RightMirrored: rotate(); break; } switch (orientationDest) { case MediaFileImageOrientation.Left: case MediaFileImageOrientation.LeftMirrored: case MediaFileImageOrientation.Right: case MediaFileImageOrientation.RightMirrored: rotate(); break; } w = retW; h = retH; }
internal override void LoadCursor(ICursor cursor, MediaAssetQueryHelper helper) { base.LoadCursor(cursor, helper); if (helper.DateTakenColumn > -1) { TakenDate = DateTimeOffset.FromUnixTimeMilliseconds(cursor.GetLong(helper.DateTakenColumn)).DateTime; } if (helper.WidthColumn > -1) { Width = cursor.GetInt(helper.WidthColumn); } if (helper.HeightColumn > -1) { Height = cursor.GetInt(helper.HeightColumn); } if (helper.LatitudeColumn > -1) { Latitude = cursor.GetDouble(helper.LatitudeColumn); } if (helper.LongitudeColumn > -1) { Longitude = cursor.GetDouble(helper.LongitudeColumn); } if (helper.OrientationColumn > -1) { switch (cursor.GetInt(helper.OrientationColumn)) { case 90: Orientation = MediaFileImageOrientation.Left; break; case 180: Orientation = MediaFileImageOrientation.Down; break; case 270: Orientation = MediaFileImageOrientation.Right; break; default: Orientation = MediaFileImageOrientation.Up; break; } } }
private static Bitmap ApplyImageOptions(Bitmap img, MediaFileImageOrientation originalOrientation, MediaFileGetImageOptions options, float w, float h) { RotateDimension(w, h, originalOrientation, options.Orientation, out w, out h); if (w <= 0) { if (h > 0) { w = img.Width * h / img.Height; } else { w = img.Width; } } if (h <= 0) { h = img.Height * w / img.Width; } GetResizeDimensions(img.Width, img.Height, w, h, options.ResizeAspect, out var widthDest, out var heightDest, out var x, out var y, out w, out h); if (img.Width != widthDest || img.Height != heightDest) { var newImg = Bitmap.CreateScaledBitmap(img, (int)widthDest, (int)heightDest, false); img.Dispose(); img = newImg; } var matrix = GetOrientationMatrix(originalOrientation, options.Orientation); if (matrix != null || x != 0 || y != 0 || w != img.Width || h != img.Height) { var newImg = Bitmap.CreateBitmap(img, (int)x, (int)y, (int)w, (int)h, matrix, false); img.Dispose(); return(newImg); } return(img); }
private static Matrix GetOrientationMatrix(MediaFileImageOrientation originalOrientation, MediaFileImageOrientation orientationDest) { var rotation = 0f; var scaleX = 1f; var scaleY = 1f; switch (originalOrientation) { case MediaFileImageOrientation.Down: rotation = 180f; break; case MediaFileImageOrientation.Left: rotation = 90f; break; case MediaFileImageOrientation.Right: rotation = 270f; break; } if (orientationDest != MediaFileImageOrientation.Up) { switch (orientationDest) { case MediaFileImageOrientation.Up: break; case MediaFileImageOrientation.UpMirrored: scaleX = -1f; break; case MediaFileImageOrientation.Down: rotation += 180f; break; case MediaFileImageOrientation.DownMirrored: rotation += 180f; scaleX = -1f; break; case MediaFileImageOrientation.Left: rotation += 270f; break; case MediaFileImageOrientation.LeftMirrored: rotation += 270f; scaleY = -1f; break; case MediaFileImageOrientation.Right: rotation += 90f; break; case MediaFileImageOrientation.RightMirrored: rotation += 90f; scaleY = -1f; break; default: break; } } Matrix matrix = null; if (rotation != 0f) { matrix = new Matrix(); matrix.PostRotate(rotation); } if (scaleX != 1f || scaleY != 1f) { matrix = matrix ?? new Matrix(); matrix.PostScale(scaleX, scaleY); } return(matrix); }