作成日: 2021/02/26 最終更新日: 2021/02/26
文書種別
使用方法
詳細
Jpg画像を回転すると、回転の情報はExif情報としてjpgファイルに保存されます。しかし、C1PictureBoxのImageプロパティにこの画像を設定しても、C1PictureBoxにはExif情報を参照する機能がないため、回転前の画像が表示されます。
Jpg画像を回転したままの状態で表示するには、まず画像ファイルから回転方向のプロパティタグの値(*1)を読み取ったのち、それに応じて手動で画像を回転(*2)してから表示します。
*1 回転方向プロパティタグ(タグID=274)の値は、System.Drawing.ImageクラスのGetPropertyItemメソッドを使用して取得できます。
*2 画像を回転するには、System.Drawing.ImageクラスのRotateFlipメソッドを使用します。
◎サンプルコード(VB)
Jpg画像を回転したままの状態で表示するには、まず画像ファイルから回転方向のプロパティタグの値(*1)を読み取ったのち、それに応じて手動で画像を回転(*2)してから表示します。
*1 回転方向プロパティタグ(タグID=274)の値は、System.Drawing.ImageクラスのGetPropertyItemメソッドを使用して取得できます。
*2 画像を回転するには、System.Drawing.ImageクラスのRotateFlipメソッドを使用します。
◎サンプルコード(VB)
Public Class Form1
Private img As System.Drawing.Image
Private Sub Form_Load() Handles Me.Load
img = System.Drawing.Image.FromFile("..¥..¥Test.jpg")
img.RotateFlip(GetExifOrientation(img))
C1PictureBox1.Image = img
End Sub
Private Function GetExifOrientation(ByVal image As System.Drawing.Image) As RotateFlipType
If image Is Nothing Then Return Nothing
Dim orientationId As Integer = 274
If image.PropertyIdList.Contains(orientationId) Then
Dim orientation As Integer = CInt(image.GetPropertyItem(orientationId).Value(0))
Dim rotateFlip As System.Drawing.RotateFlipType = RotateFlipType.RotateNoneFlipNone
Select Case orientation
Case 1
rotateFlip = RotateFlipType.RotateNoneFlipNone
Case 2
rotateFlip = RotateFlipType.RotateNoneFlipX
Case 3
rotateFlip = RotateFlipType.Rotate180FlipNone
Case 4
rotateFlip = RotateFlipType.Rotate180FlipX
Case 5
rotateFlip = RotateFlipType.Rotate90FlipX
Case 6
rotateFlip = RotateFlipType.Rotate90FlipNone
Case 7
rotateFlip = RotateFlipType.Rotate270FlipX
Case 8
rotateFlip = RotateFlipType.Rotate270FlipNone
Case Else
rotateFlip = RotateFlipType.RotateNoneFlipNone
End Select
Return rotateFlip
End If
Return Nothing
End Function
End Class
◎サンプルコード(C#) public partial class Form1 : Form
{
System.Drawing.Image img;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
img = System.Drawing.Image.FromFile("..¥¥..¥¥Test.jpg");
img.RotateFlip(GetExifOrientation(img));
c1PictureBox1.Image = img;
}
private RotateFlipType GetExifOrientation(System.Drawing.Image image)
{
if ((image == null))
{
return RotateFlipType.RotateNoneFlipNone;
}
int orientationId = 274;
if (image.PropertyIdList.Contains(orientationId))
{
int orientation = int.Parse(image.GetPropertyItem(orientationId).Value[0].ToString());
System.Drawing.RotateFlipType rotateFlip = RotateFlipType.RotateNoneFlipNone;
switch (orientation)
{
case 1:
rotateFlip = RotateFlipType.RotateNoneFlipNone;
break;
case 2:
rotateFlip = RotateFlipType.RotateNoneFlipX;
break;
case 3:
rotateFlip = RotateFlipType.Rotate180FlipNone;
break;
case 4:
rotateFlip = RotateFlipType.Rotate180FlipX;
break;
case 5:
rotateFlip = RotateFlipType.Rotate90FlipX;
break;
case 6:
rotateFlip = RotateFlipType.Rotate90FlipNone;
break;
case 7:
rotateFlip = RotateFlipType.Rotate270FlipX;
break;
case 8:
rotateFlip = RotateFlipType.Rotate270FlipNone;
break;
default:
rotateFlip = RotateFlipType.RotateNoneFlipNone;
break;
}
return rotateFlip;
}
return RotateFlipType.RotateNoneFlipNone;
}
}
旧文書番号
86453