C#图片旋转

本文介绍了一种根据图片EXIF信息自动调整图片方向的方法,并强调了保存图片时选择正确格式的重要性,避免图片体积过大。

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

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)); //保存的时候必须添加图片格式,不然图片会很大;
    }

    /// <summary> 
    /// 根据图片exif调整方向 
    /// </summary> 
    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#实现图片旋转有多种方法,以下是详细介绍: #### 方法一:通过像素遍历实现旋转 此方法通过加载图片,创建新的位图,遍历源图片的每个像素,计算旋转后的像素位置并赋值给新位图,最后保存旋转后的图片。 ```csharp using System; using System.Drawing; class Program { static void Main() { // 加载需要旋转图片 Bitmap sourceImage = new Bitmap("input.jpg"); // 创建一个新的位图用于存储旋转后的图片 Bitmap rotatedImage = new Bitmap(sourceImage.Height, sourceImage.Width); // 循环遍历源图片的每一个像素 for (int i = 0; i < sourceImage.Width; i++) { for (int j = 0; j < sourceImage.Height; j++) { // 计算旋转后的像素位置 int rotatedX = j; int rotatedY = sourceImage.Width - i - 1; // 将源图片的像素颜色赋值给旋转后的图片 rotatedImage.SetPixel(rotatedX, rotatedY, sourceImage.GetPixel(i, j)); } } // 保存旋转后的图片 rotatedImage.Save("output.jpg"); Console.WriteLine("图片旋转成功!"); } } ``` 这种方法的核心在于通过双重循环遍历源图片的每个像素,并根据旋转规则计算新的像素位置,将原像素颜色复制到新位置上,实现图片旋转,可参考引用[^1]。 #### 方法二:使用Graphics的TranslateTransform和RotateTransform方法 通过设置旋转的中心坐标和画笔的旋转角度来实现图片旋转。 ```csharp using System; using System.Drawing; class Program { public static Bitmap rotateImage(Bitmap b, float angle) { // create a new empty bitmap to hold rotated image Bitmap returnBitmap = new Bitmap(b.Width, b.Height); // make a graphics object from the empty bitmap Graphics g = Graphics.FromImage(returnBitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; // move rotation point to center of image g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2); // rotate g.RotateTransform(angle); // move image back g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2); // draw passed in image onto graphics object g.DrawImage(b, new Point(0, 0)); return returnBitmap; } } ``` 该方法先创建一个新的空位图,再从该位图创建`Graphics`对象。通过`TranslateTransform`方法将旋转点移动到图像中心,然后使用`RotateTransform`方法进行旋转,最后再将图像移回原来的位置并绘制图像,可参考引用[^4]。 #### 方法三:在面板上动态旋转显示图片 在面板上以任意角度旋转显示图像。 ```csharp using System; using System.Drawing; using System.Windows.Forms; using System.Threading; private void button1_Click(object sender, EventArgs e) { // 以任意角度旋转显示图像 Graphics g = this.panel1.CreateGraphics(); float MyAngle = 0; // 旋转的角度 while (MyAngle < 360) { TextureBrush MyBrush = new TextureBrush(MyBitmap); this.panel1.Refresh(); MyBrush.RotateTransform(MyAngle); g.FillRectangle(MyBrush, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); MyAngle += 0.5f; Thread.Sleep(50); } } ``` 此方法在按钮点击事件中,通过`CreateGraphics`方法获取面板的`Graphics`对象,使用`TextureBrush`来绘制旋转的图像,通过循环不断改变旋转角度并刷新面板,实现动态旋转显示,可参考引用[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值