You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET/C#实现扫描图片自动旋转功能:求推荐相关类库

.NET/C# 实现扫描图片自动旋转的类库推荐

针对扫描图片的自动旋转需求,以下是适合.NET/C#场景的免费和付费类库方案:

免费类库

1. System.Drawing.Common

这是.NET生态中最基础的图像处理类库(.NET 5及以上需单独安装System.Drawing.Common NuGet包),核心是利用图片的EXIF方向标签实现自动旋转——多数扫描设备会在生成图片时记录旋转信息。

示例代码:

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

public static void AutoRotateImage(string inputPath, string outputPath)
{
    using (var image = Image.FromFile(inputPath))
    {
        // 读取EXIF旋转信息
        var propItem = image.GetPropertyItem(274); // 274是EXIF方向标签ID
        int orientation = BitConverter.ToInt16(propItem.Value, 0);

        // 根据方向值执行旋转
        switch (orientation)
        {
            case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break;
            case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
            case 4: image.RotateFlip(RotateFlipType.Rotate180FlipX); break;
            case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); break;
            case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
            case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); break;
            case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
        }

        // 保存时移除EXIF标签,避免后续重复旋转
        image.RemovePropertyItem(274);
        image.Save(outputPath, ImageFormat.Jpeg);
    }
}

2. ImageSharp

跨平台的开源图像处理库(支持.NET Core/.NET 5+),无需依赖GDI+,原生支持读取EXIF并自动应用旋转,适合跨平台项目。

示例代码:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

public static void AutoRotateWithImageSharp(string inputPath, string outputPath)
{
    using (var image = Image.Load(inputPath))
    {
        // 自动应用EXIF旋转
        image.Mutate(x => x.AutoOrient());
        image.Save(outputPath);
    }
}

3. OpenCVSharp

基于OpenCV的.NET封装库,适合无EXIF标签的扫描图片——可以通过图像内容分析(比如文本倾斜检测)自动计算旋转角度,适合复杂场景。

核心思路:通过霍夫变换检测直线角度,或使用文本检测模型识别文本方向,再执行旋转。示例代码(简化版):

using OpenCvSharp;

public static void AutoRotateByContent(string inputPath, string outputPath)
{
    using (var src = Cv2.ImRead(inputPath, ImreadModes.Grayscale))
    {
        // 边缘检测
        using (var edges = new Mat())
        {
            Cv2.Canny(src, edges, 50, 150);
            // 霍夫变换检测直线
            var lines = Cv2.HoughLinesP(edges, 1, Math.PI / 180, 50, minLineLength: 50, maxLineGap: 10);
            
            // 计算平均角度
            double totalAngle = 0;
            int count = 0;
            foreach (var line in lines)
            {
                var x1 = line[0]; var y1 = line[1]; var x2 = line[2]; var y2 = line[3];
                double angle = Math.Atan2(y2 - y1, x2 - x1) * 180 / Math.PI;
                // 过滤垂直/水平以外的角度
                if (Math.Abs(angle) > 10 && Math.Abs(angle) < 80)
                {
                    totalAngle += angle;
                    count++;
                }
            }
            if (count > 0)
            {
                double avgAngle = totalAngle / count;
                // 旋转图像
                using (var rotated = new Mat())
                {
                    var center = new Point2f(src.Width / 2f, src.Height / 2f);
                    var rotationMatrix = Cv2.GetRotationMatrix2D(center, avgAngle, 1.0);
                    Cv2.WarpAffine(src, rotated, rotationMatrix, src.Size, borderMode: BorderTypes.Replicate);
                    Cv2.ImWrite(outputPath, rotated);
                }
            }
        }
    }
}

付费类库

1. Leadtools SDK

专业级图像处理SDK,提供成熟的内容驱动自动旋转功能——不仅支持EXIF,还能识别图片中的文本、图形方向,自动修正旋转角度,适合企业级批量处理场景,配套完善的技术支持。核心用法是调用AutoRotateCommand API,自动检测并旋转图像。

2. DevExpress Image Processing Library

DevExpress组件套件中的图像处理模块,集成度高,提供一键式自动旋转功能,适合已经使用DevExpress控件的.NET项目,无需额外适配成本。

内容的提问来源于stack exchange,提问作者Abraham Mathew Ajoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 16:15:24