从Microsoft Paint读取矩形坐标
Curtis Chau
更新: 2026年8月21日
一些IronOCR方法接受Rectangle,以便您可以对图像的一部分而不是整个图像进行OCR。 本指南向您展示如何直接从Microsoft Paint中读取Rectangle所需的四个值(x、y、width和height)。
构造函数看起来是这样的:
Rectangle(int x, int y, int width, int height, MeasurementUnits units = MeasurementUnits.Pixels)Rectangle(x As Integer, y As Integer, width As Integer, height As Integer, Optional units As MeasurementUnits = MeasurementUnits.Pixels)Rectangle(x As Integer, y As Integer, width As Integer, height As Integer, Optional units As MeasurementUnits = MeasurementUnits.Pixels)每个值的含义:
x: 从图像左边缘到您的区域开始的距离。y: 从图像上边缘到您的区域开始的距离。width: 您要读取的区域的宽度。height: 您要读取的区域的高度。MeasurementUnits.Pixels: 告诉IronOCR这些值是以像素为单位。
坐标从图像的左上角开始。 x向右增长,y向下增长。

先决条件
- Microsoft Paint(或任何显示光标位置和像素选择大小的编辑器)
- 您想要运行OCR的图像
- 将IronOCR添加到您的项目中
这适用于版本2026.5.2及其后版本。
解决方案
1.读取X和Y起始点
在Microsoft Paint中打开图像,然后将光标移动到您希望读取区域的左上角。 那个角是您的起点。 从窗口的左下角读取光标位置。 在下方截图中显示为2403, 1653px,所以:
X = 2403
Y = 1653
Text

2.读取宽度和高度
使用选择工具,并在目标文本上拖动一个框。 Paint在左下角显示您的选择大小。 在下方截图中显示为1031 × 214px,所以:
Width = 1031
Height = 214
Text

3. 构建矩形
从您刚刚读取的四个值中组装Rectangle。 对于上面的Testing123区域:
var rectangle = new Rectangle(2403,1653,1031,214,MeasurementUnits.Pixels);var rectangle = new Rectangle(2403,1653,1031,214,MeasurementUnits.Pixels);Dim rectangle As New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)Dim rectangle As New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)将矩形传递给IronOCR,将图像裁剪到该区域:
var rectangle = new Rectangle(2403,1653,1031,214,MeasurementUnits.Pixels);
ocrInput.LoadImage(frame, rectangle);var rectangle = new Rectangle(2403,1653,1031,214,MeasurementUnits.Pixels);
ocrInput.LoadImage(frame, rectangle);Dim rectangle = New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)
ocrInput.LoadImage(frame, rectangle)Dim rectangle = New Rectangle(2403, 1653, 1031, 214, MeasurementUnits.Pixels)
ocrInput.LoadImage(frame, rectangle)注意事项和限制
- 状态栏中的
1031 × 214px值是您的选择的宽度和高度。 这不是坐标。 2403, 1653px光标读取是X和Y起始点(区域的左上角)。- 不要使用完整图像尺寸(此处为
4960 × 7014px)作为矩形大小。那样会选择整个图像,而不是您的区域。 - Paint以像素为单位报告,所以传递
MeasurementUnits.Pixels以匹配。 - 您是凭借观察读取这些值的,因此它们可能相差几像素。 如果裁剪导致部分文本被截断,可以稍微加宽
width或height,或者降低x或y。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。