티스토리 뷰

728x90

 

핸드폰에서 세로로 찍은 사진을 사진 뷰어나 그래픽 편집 도구로 확인하면 위의 그림처럼 문제없이 보인다.

 

그런데, 해당 그림을 C# PictureBox 컨트롤에 적용하면 위의 그림처럼 그림이 자동으로 회전된다. 이 문제는 PictureBox 컨트롤이 JPG나 PNG 파일에 있는 Exif(Exchangeable image file format) 형식을 제대로 지원하지 않아서 생기는 것이다. 핸드폰이나 기타 도구에서 이미지를 저장할 때 가로 형태로 이미지를 저장하고 Orientation 정보(0x112)에 이미지는 90도 회전했다는 식으로 설정했는데 PictureBox 컨트롤이 그 정보를 처리하지 않는다는 것이다.

 

public class MyPictureBox : PictureBox
{
	private void CorrectExifOrientation(Image image)
	{
		if (image == null) return;
		int orientationId = 0x0112;
		if (image.PropertyIdList.Contains(orientationId))
		{
			var orientation = (int)image.GetPropertyItem(orientationId).Value[0];
			var 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;
			}
			if (rotateFlip != RotateFlipType.RotateNoneFlipNone)
			{
				image.RotateFlip(rotateFlip);
				image.RemovePropertyItem(orientationId);
			}
		}
	}
	[Localizable(true)]
	[Bindable(true)]
	public new Image Image
	{
		get { return base.Image; }
		set { base.Image = value; CorrectExifOrientation(value); }
	}
}

 

문제 해결 방법은 PictureBox 클래스를 상속하는 사용자 클래스를 만들어 기존의 PictureBox를 대체하는 것이다. *.Designer.cs 파일에서 System.Windows.Forms.PictureBox를 MyPictureBox로 변경하면 된다.

 

 

위의 그림은 MyPictureBox를 적용 이후의 결과이다.

 

 

 

 

728x90
댓글
최근에 올라온 글
최근에 달린 댓글
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함