본문 바로가기

C#/Image | Video

이미지 EXIF 정보 가져오기, 이미지 회전 후 저장

이미지 EXIF 정보 가져오기, 이미지 회전 후 저장

 

 

Get Image EXIF Information / Rotate Image and Save

public ActionResult Pictures(string id)
{
    PictureModel pictureModel = new PictureModel();
    pictureModel._ID = id;
    pictureModel._Images = new List<string>();
    pictureModel._ImagesOrigin = new List<string>();
 
    if (id == null)
    {
        return View(pictureModel);
    }
 
    // 웹 서버 실제 파일 경로
    // Server.MapPath(".") .이 늘어날 수록 부모 디렉토리를 반환한다.
    var pictureDirPath = Server.MapPath("..") + "/Pictures";
    DirectoryInfo di = new DirectoryInfo(pictureDirPath + "/" + pictureModel._ID);
    if (di.Exists)
    {
        var files = di.GetFiles();
        foreach (var file in files)
        {
            using (var image = System.Drawing.Image.FromFile(file.FullName))
            {
                if (image.PropertyIdList.Contains(0x112))
                {
                    foreach (var prop in image.PropertyItems)
                    {
                        if (prop.Id == 0x112) // EXIF 회전 정보 아이디 (274)
                        {
                            //사진 정보가 90도 회전일 경우
                            //원본 사진 경로에 'rotate' 폴더를 생성 후 'rotate' 폴더 안에 -90도의 사진을 저장한다.
                            //prop.Value[0] == 6 : 90도
                            //prop.Value[0] == 8 : -90도
                            //prop.Value[0] == 3 : 180도
                            if (prop.Value[0] == 6)
                            {
                                DirectoryInfo di_rotate = new DirectoryInfo(file.DirectoryName + "\\rotate");
                                if (di_rotate.Exists == false)
                                {
                                    di_rotate.Create();
                                }
                                string rotateFilePath = di_rotate.FullName + "\\rotate_" + file.Name;
                                FileInfo fi = new FileInfo(rotateFilePath);
                                if (fi.Exists == false)
                                {
                                    RotateImage(file.FullName, rotateFilePath, RotateFlipType.Rotate90FlipNone);
                                }
 
                                pictureModel._Images.Add("/Pictures/" + pictureModel._ID + "/" + "rotate/" + "rotate_" + file.Name);
                            }
                            else if (prop.Value[0] == 8)
                            {
                                DirectoryInfo di_rotate = new DirectoryInfo(file.DirectoryName + "\\rotate");
                                if (di_rotate.Exists == false)
                                {
                                    di_rotate.Create();
                                }
                                string rotateFilePath = di_rotate.FullName + "\\rotate_" + file.Name;
                                FileInfo fi = new FileInfo(rotateFilePath);
                                if (fi.Exists == false)
                                {
                                    RotateImage(file.FullName, rotateFilePath, RotateFlipType.Rotate270FlipNone);
                                }
 
                                pictureModel._Images.Add("/Pictures/" + pictureModel._ID + "/" + "rotate/" + "rotate_" + file.Name);
                            }
                            else
                            {
                                pictureModel._Images.Add("/Pictures/" + pictureModel._ID + "/" + file.Name);
                            }
 
                            pictureModel._RotationVal = prop.Value[0];
                            pictureModel._ImagesOrigin.Add("/Pictures/" + pictureModel._ID + "/" + file.Name);
 
                            break;
                        }
                    }
                }
                else
                {
                    // EXIF 회전 정보가 없을 경우 
                    pictureModel._Images.Add("/Pictures/" + pictureModel._ID + "/" + file.Name);
                    pictureModel._ImagesOrigin.Add("/Pictures/" + pictureModel._ID + "/" + file.Name);
                }
            }
 
        }
    }
 
    return View(pictureModel);
}
 
public void RotateImage(String input, String output, RotateFlipType type)
{
    Image img = Image.FromFile(input);
    var bmp = new Bitmap(img);
 
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        gfx.Clear(Color.White);
        gfx.DrawImage(img, 0, 0, img.Width, img.Height);
    }
 
    bmp.RotateFlip(type);
    bmp.Save(output);
}

'C# > Image | Video' 카테고리의 다른 글

C# Byte To ImageSource  (0) 2020.07.30
C# Image To Byte  (0) 2020.07.27
C# 화면 캡쳐 Capture  (0) 2020.07.27
C# lock 없이 비트맵 이미지 픽셀 Get/Set  (0) 2020.07.27