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

C# WinForms截图在IronOCR中DPI异常导致识别不佳的解决方法

解决IronOcr/TesseractOcr识别时的DPI异常问题

问题根源

你遇到的Invalid resolution 3780 dpi警告,本质是截图的实际像素密度与DPI元数据不匹配:

  • CopyFromScreen生成的Bitmap默认沿用屏幕DPI,但高DPI系统下WinForms的逻辑坐标与实际像素坐标存在缩放差,导致截图的DPI元数据异常;
  • 单纯调用SetResolution仅修改图片元数据,并未提升实际像素密度,OCR库仍会因实际像素不足降级处理。

解决方案

方案1:截图时直接生成300DPI的图像(推荐)

修改截图方法,先处理高DPI缩放,确保截图的实际像素数量与300DPI匹配,同时设置正确的DPI元数据:

private Bitmap CaptureImage()
{
    // 获取屏幕的DPI缩放因子(适配高DPI系统)
    using (Graphics screenGraphics = Graphics.FromHwnd(IntPtr.Zero))
    {
        float dpiScaleX = screenGraphics.DpiX / 96f;
        float dpiScaleY = screenGraphics.DpiY / 96f;

        // 将WinForms逻辑坐标的captureArea转换为实际屏幕像素坐标
        int actualWidth = (int)(captureArea.Width * dpiScaleX);
        int actualHeight = (int)(captureArea.Height * dpiScaleY);
        Point actualLocation = new Point((int)(captureArea.X * dpiScaleX), (int)(captureArea.Y * dpiScaleY));

        // 创建目标Bitmap并直接设置300DPI
        Bitmap capturedImage = new Bitmap(actualWidth, actualHeight);
        capturedImage.SetResolution(300, 300);

        using (Graphics graphics = Graphics.FromImage(capturedImage))
        {
            // 启用高质量绘图模式
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            // 复制实际屏幕像素到Bitmap
            graphics.CopyFromScreen(actualLocation, Point.Empty, new Size(actualWidth, actualHeight));
        }

        return capturedImage;
    }
}

方案2:将现有截图缩放至300DPI等效像素尺寸

如果无法修改原截图逻辑,可在截图后对图像进行缩放,确保实际像素密度达到300DPI要求:

private Bitmap ScaleTo300Dpi(Bitmap originalImage)
{
    // 计算缩放比例:目标300DPI / 原始图像DPI
    float scaleRatio = 300f / originalImage.HorizontalResolution;

    // 创建缩放后的Bitmap,设置300DPI
    Bitmap scaledImage = new Bitmap(
        (int)(originalImage.Width * scaleRatio),
        (int)(originalImage.Height * scaleRatio)
    );
    scaledImage.SetResolution(300, 300);

    // 高质量绘制缩放后的图像
    using (Graphics g = Graphics.FromImage(scaledImage))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(originalImage, 0, 0, scaledImage.Width, scaledImage.Height);
    }

    return scaledImage;
}

配合IronOcr的正确使用方式

处理后的图像传入OCR时,可显式指定TargetDpi确保识别效果:

var ocr = new IronTesseract();
using (var processedImage = CaptureImage()) // 或ScaleTo300Dpi(原截图)
using (var ocrInput = new OcrInput(processedImage))
{
    ocrInput.TargetDpi = 300;
    var result = ocr.Read(ocrInput);
}

额外配置:启用WinForms高DPI适配

在Program.cs的Main方法开头添加以下代码,避免高DPI系统下控件坐标偏移:

Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

内容的提问来源于stack exchange,提问作者LaysYogurt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:23:23