close

要引用的

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

 

note: 圖片的exif  有帶資訊才轉的動 囧

沒有的話,也可以依此function  稍改一下來手動轉

 

function 用法

     System.Drawing.Image Rotoimage = System.Drawing.Image.FromFile(Server.MapPath(tmp_ImgUrl));
        try
        {
            RotateImage(Rotoimage, Server.MapPath(tmp_ImgUrl));
        }
        finally
        {
            Rotoimage.Dispose();
        }

 

function 在此

 

        /// <summary>
        /// 自動轉正圖片
        /// </summary>
        /// <param name="img">image</param>
        /// <param name="imgpath">存檔path與檔名</param>
        public static void RotateImage(System.Drawing.Image img, string imgpath )
        {
            PropertyItem[] exif = img.PropertyItems;
            byte orientation = 0;
            foreach (PropertyItem i in exif)
            {
                if (i.Id == 274)
                {
                    orientation = i.Value[0];
                    i.Value[0] = 1;
                    img.SetPropertyItem(i);
                }
            }

            switch (orientation)
            {
               
             
                case 2:
                    img.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    break;
                case 3:
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;
                case 4:
                    img.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    break;
                case 5:
                    img.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;
                case 6:
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;
                case 7:
                    img.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case 8:
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                default:
                    return;
                    break;
            }

            foreach (PropertyItem i in exif)
            {
                if (i.Id == 40962)
                {
                    i.Value = BitConverter.GetBytes(img.Width);
                }
                else if (i.Id == 40963)
                {
                    i.Value = BitConverter.GetBytes(img.Height);
                }
                img.SetPropertyItem(i);
            }





            #region Configure JPEG Compression Engine



            System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();

            long[] quality = new long[1];

            quality[0] = 100;

            System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

            encoderParams.Param[0] = encoderParam;



            System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();

            System.Drawing.Imaging.ImageCodecInfo jpegICI = null;

            for (int x = 0; x < arrayICI.Length; x++)
            {

                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {

                    jpegICI = arrayICI[x];

                    break;

                }

            }



            #endregion

            img.Save(imgpath, jpegICI, encoderParams);
          
        }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(2) 人氣()