從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。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。