C#图片旋转

保存图片的时候一定要选择图片保存的格式

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;

public partial class AS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Drawing.Image img = null;
        string fullpath = Server.MapPath("~/content/originimg.jpg");
        //img = RotateImage(System.Drawing.Image.FromStream(postedFile.InputStream));
        img = RotateImage(System.Drawing.Image.FromFile(fullpath));

        int Width = img.Width;
        int Height = img.Height;

        //设置文件名
        string fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(fullpath);
        //保存文件
        img.Save(Server.MapPath("~/content/" + fileNewName), GetImageFormat(fullpath)); //保存的时候必须添加图片格式,不然图片会很大;
    }

    ///  
    /// 根据图片exif调整方向 
    ///  
    private Image RotateImage(Image tmpbitmap)
    {
        try
        {
            var exif = tmpbitmap.PropertyItems;
            byte orien = 0;
            var item = exif.Where(m => m.Id == 274).ToArray();
            if (item.Length > 0)
            {
                orien = item[0].Value[0];
            }

            switch (orien)
            {
                case 2:
                    tmpbitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); //horizontal flip 
                    break;
                case 3:
                    tmpbitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); //right-top 
                    break;
                case 4:
                    tmpbitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); //vertical flip 
                    break;
                case 5:
                    tmpbitmap.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;
                case 6:
                    tmpbitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); //right-top 
                    break;
                case 7:
                    tmpbitmap.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case 8:
                    tmpbitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); //left-bottom 
                    break;
                default:
                    break;
            }

            Bitmap bitmap = new Bitmap(tmpbitmap);
            tmpbitmap.Dispose();
            return (Image) bitmap;
        }
        catch (Exception E)
        {
            Response.Write("异常:" + E.Message);
            return tmpbitmap;
        }
    }

    private ImageFormat GetImageFormat(string path)
    {
        string extension = Path.GetExtension(path).ToLower();

        if (extension.Equals(".jpeg") || extension.Equals(".jpg"))
            return ImageFormat.Jpeg;
        if (extension.Equals(".bmp"))
            return ImageFormat.Bmp;
        if (extension.Equals(".png"))
            return ImageFormat.Png;
        else
            return ImageFormat.Jpeg;
    }
}

如图:

C#图片旋转_第1张图片

你可能感兴趣的:(ASP.NET,Webform)