コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using System;
using System.ComponentModel;
using Android.Graphics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(Image), typeof(AppName.Droid.Renderer.CustomImageRenderer))]
namespace AppName.Droid.Renderer
{
public class CustomImageRenderer : ImageRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Control == null)
{
return;
}
ImageSource source = Element.Source;
var width = (int)Element.Width;
var height = (int)Element.Height;
Bitmap bitmap = null;
if (typeof(FileImageSource) == source.GetType())
{
var filePath = ((FileImageSource)source).File;
Android.Media.ExifInterface exifInterface = new Android.Media.ExifInterface(filePath);
// 向きを取得
int orientation = Int32.Parse(exifInterface.GetAttribute(Android.Media.ExifInterface.TagOrientation));
Matrix matrix = new Matrix();
matrix.Reset();
switch (orientation)
{
case (int)Android.Media.Orientation.Undefined: // 0:
break;
case (int)Android.Media.Orientation.Normal: // 1:
break;
case (int)Android.Media.Orientation.FlipHorizontal: // 2:
break;
case (int)Android.Media.Orientation.Rotate180: // 3:
matrix.PostRotate(180f);
break;
case (int)Android.Media.Orientation.FlipVertical: // 4:
break;
case (int)Android.Media.Orientation.Transpose: // 5:
break;
case (int)Android.Media.Orientation.Rotate90: // 6:
matrix.PostRotate(90f);
break;
case (int)Android.Media.Orientation.Transverse: // 7:
break;
case (int)Android.Media.Orientation.Rotate270: // 8:
matrix.PostRotate(270f);
break;
}
var options = new BitmapFactory.Options { InJustDecodeBounds = false };
bitmap = BitmapFactory.DecodeFile(filePath, options);
bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
}
//オリジナルのイメージコントロールに修正した画像ファイルをセットします。
Control.SetImageBitmap(bitmap);
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。