IronOCR 操作指南 System.Drawing 對象 如何在 C# 中從 System.Drawing 物件讀取資料# Curtis Chau 更新:2026年1月10日 下載 IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronOCR 透過將 Bitmap 和 Image 等物件包裝在 OcrImageInput 中,可從 System.Drawing 物件讀取文字,為 .NET 應用程式提供跨 Windows、macOS 和 Linux 平台的無縫 OCR 功能。 System.Drawing.Bitmap是 .NET Framework 中的一個類,用於處理點陣圖映像。 它提供了建立、操作和顯示點陣圖影像的方法和屬性。 System.Drawing.Image是 .NET Framework 中所有 GDI+ 映像物件的基底類別。 它是各種影像類型的父類,包括System.Drawing.Bitmap 。 IronSoftware.Drawing.AnyBitmap是IronDrawing中的一個點陣圖類,IronDrawing 是一個開源函式庫,最初由 Iron Software 開發。 它可以幫助 C# 軟體工程師在 Windows、macOS 和 Linux 平台上的 .NET 專案中取代System.Drawing.Common 。 快速入門:從 System.Drawing.Bitmap 讀取文字 使用單一語句,建立一個 IronTesseract 並輸入一個由 OcrImageInput 包覆的 System.Drawing.Bitmap 來擷取所有文字。 此快速入門範例展示 IronOCR 如何以最少的設定將影像轉換成可讀的文字。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronOCR PM > Install-Package IronOcr 複製並運行這段程式碼。 var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(new System.Drawing.Bitmap("image.png"))); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronOCR,免費試用! 免費試用30天 ## 如何在 C# 中讀取 System.Drawing 對象 下載用於讀取 System.Drawing 物件的 C# 函式庫 Obtain System.Drawing objects such as `Bitmap` and `Image` Construct the `OcrImageInput` class using the acquired data Utilize `AnyBitmap` from Iron Software for Linux and macOS 通过指定裁剪区域定义读取区域 如何從 System.Drawing.Bitmap 讀取? <! -- --> <! <!--說明:顯示逐步過程的截圖 --> 首先,實體化 IronTesseract 類別以執行 OCR。 使用各種方法之一建立System.Drawing.Bitmap 。 在程式碼範例中,使用了檔案路徑。 接下來,使用using語句建立OcrImageInput對象,並將System.Drawing.Bitmap物件中的圖像傳遞給它。 最後,使用Read方法執行 OCR。 :path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-bitmap.cs using IronOcr; using System.Drawing; // Instantiate IronTesseract IronTesseract ocrTesseract = new IronTesseract(); // Read image file to Bitmap Bitmap bitmap = new Bitmap("Potter.tiff"); // Import System.Drawing.Bitmap using var imageInput = new OcrImageInput(bitmap); // Perform OCR OcrResult ocrResult = ocrTesseract.Read(imageInput); Imports IronOcr Imports System.Drawing ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Read image file to Bitmap Private bitmap As New Bitmap("Potter.tiff") ' Import System.Drawing.Bitmap Private imageInput = New OcrImageInput(bitmap) ' Perform OCR Private ocrResult As OcrResult = ocrTesseract.Read(imageInput) $vbLabelText $csharpLabel 為何 OcrImageInput 的 using 語句很重要? 在使用 OcrImageInput 時,using 語句非常重要,因為它可以確保適當的資源管理和記憶體清理。OcrImageInput 實作IDisposable,這表示它持有未管理的資源,當您使用完該物件時,需要釋放這些資源。 如果沒有 using 語句,這些資源可能無法即時釋放,可能導致記憶體洩漏或檔案鎖定。 在批次作業中處理多個影像時,這一點尤其重要。 有關 IronOCR 中正確資源管理的詳細資訊,請參閱我們的 API Reference 文件。 常見的 Bitmap 載入方法有哪些? System.Drawing.Bitmap除了我們範例中使用的檔案路徑建構器外,還提供多種載入方法。 您可以從資料流(new Bitmap(stream))、現有的 Images (new Bitmap(image))中建立 Bitmaps ,甚至可以建立具有特定尺寸的空白位圖(new Bitmap(width,height))。 在使用 Web 應用程式時,從串流載入對於處理上傳的檔案特別有用。 對於嵌入式資源,您可以使用 Assembly.GetManifestResourceStream() 。 IronOCR 可透過 OcrImageInput 建構器來無縫處理所有這些 Bitmap 來源。 在我們的 圖片 (jpg, png, gif, tiff, bmp) 指南中瞭解更多關於不同輸入方法的資訊。 我應該何時拋棄 Bitmap 物件? 位圖處理的時間取決於您應用程式的工作流程。 如果您只需要 Bitmap 用於 OCR,請在建立 OcrImageInput 之後立即將它丟棄。 但是,如果需要執行多個作業或顯示圖片,請保持圖片的活力,直到所有作業完成為止。 務必使用 using 語句或 try-finally 區塊,以確保處理。 請記住,OcrImageInput 會建立自己的內部複本,因此原始的 Bitmap 可以在 OcrImageInput 建立之後丟棄。 對於涉及多種影像操作的複雜情況,請考慮我們的 OCR 影像最佳化篩選器範例。 如何從 System.Drawing.Image 讀取? <! -- --> <!--說明:顯示逐步過程的截圖 --> 從 System.Drawing.Image 讀取非常簡單,只要使用 Image 建立 OcrImageInput 物件,然後使用 Read 方法執行標準 OCR 程序即可。 :path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-image.cs using IronOcr; using Image = System.Drawing.Image; // Instantiate IronTesseract IronTesseract ocrTesseract = new IronTesseract(); // Open image file as Image Image image = Image.FromFile("Potter.tiff"); // Import System.Drawing.Image using var imageInput = new OcrImageInput(image); // Perform OCR OcrResult ocrResult = ocrTesseract.Read(imageInput); Imports IronOcr Imports Image = System.Drawing.Image ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Open image file as Image Private image As Image = Image.FromFile("Potter.tiff") ' Import System.Drawing.Image Private imageInput = New OcrImageInput(image) ' Perform OCR Private ocrResult As OcrResult = ocrTesseract.Read(imageInput) $vbLabelText $csharpLabel OCR 的 Image 和 Bitmap 有何差異? System.Drawing.Bitmap 是位圖影像的特定實作,而 System.Drawing.Image 則是抽象的基類,可表示各種影像格式,包括 JPEG、PNG、GIF 和 TIFF。 就 OCR 而言,IronOCR 透過 OcrImageInput 對兩者進行相同的處理,但 Image 在處理不同格式時提供了更大的彈性。 Bitmap 提供像素層級的操作功能,而 Image 則更適合一般的影像處理。 這兩種工具在 IronOCR 先進的 Tesseract 5 引擎下運作同樣良好。選擇取決於您更廣泛的應用需求,而非 OCR 性能。 為何使用 Image.FromFile 而非其他載入方法? Image.FromFile 是從磁碟載入影像的最簡單、最直接的方法。 它會自動偵測影像格式並處理檔案讀取程序。 替代方法如 Image.FromStream 更適合網路應用程式或處理記憶體串流時使用。 Image.FromFile 鎖定檔案,直到 Image 被處置為止,這在多執行緒應用程式中可能是一個考量。 對於需要高效能或並發存取的生產情境,可考慮先將影像載入記憶體串流。我們的 Multithreaded Tesseract OCR 實例展示了並發影像處理的最佳實務。 如何從 IronSoftware.Drawing.AnyBitmap 讀取? 同樣地,在建立或取得AnyBitmap物件之後,您可以建構OcrImageInput類別。 建構器會處理匯入資料的所有必要步驟。 下面的程式碼範例演示了這一點。 :path=/static-assets/ocr/content-code-examples/how-to/input-system-drawing-read-anybitmap.cs using IronOcr; using IronSoftware.Drawing; // Instantiate IronTesseract IronTesseract ocrTesseract = new IronTesseract(); // Open image file as AnyBitmap AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff"); // Import IronSoftware.Drawing.AnyBitmap using var imageInput = new OcrImageInput(anyBitmap); // Perform OCR OcrResult ocrResult = ocrTesseract.Read(imageInput); Imports IronOcr Imports IronSoftware.Drawing ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Open image file as AnyBitmap Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff") ' Import IronSoftware.Drawing.AnyBitmap Private imageInput = New OcrImageInput(anyBitmap) ' Perform OCR Private ocrResult As OcrResult = ocrTesseract.Read(imageInput) $vbLabelText $csharpLabel 為何選擇 AnyBitmap 而非 System.Drawing 類別? 與 System.Drawing 類別相比,AnyBitmap 提供優異的跨平台相容性。 在 .NET 6+ 中,System.Drawing.Common 對非 Windows 平台的支援有限,而 AnyBitmap 則可在 Windows、Linux 和 macOS 上無縫運作。 它提供一致的 API,沒有特定平台的依賴性,因此非常適合雲端部署和容器化應用程式。 AnyBitmap 也提供更好的記憶體管理以及專為影像處理任務所設計的效能最佳化。 如需詳細的相容性資訊,請參閱我們的 相容性文件。 AnyBitmap 支援哪些平台? AnyBitmap 支援 .NET 執行的所有主要平台:Windows(x86、x64、ARM)、Linux(包括適用於 Docker 的 Alpine Linux)和 macOS(包括 Intel 和 Apple Silicon)。 這種廣泛的平台支援使其成為需要在多樣化環境中執行的現代 .NET 應用程式的推薦選擇。 對於 AWS Lambda 或 Azure Functions 的雲端部署尤其有價值。 在我們針對Linux、macOS和Docker環境的指南中,瞭解更多關於特定平台設定的資訊。 AnyBitmap 如何處理記憶體管理? AnyBitmap透過自動垃圾回收整合與明確的棄置模式來實現有效率的記憶體管理。 它使用記憶體池來處理頻繁分配的緩衝區,並實現寫入時複製的語義,以獲得更好的效能。 不像 System.Drawing.Bitmap 會持有檔案鎖,AnyBitmap 會將影像完全載入記憶體,避免檔案存取問題。 它還可以在高吞吐量的情況下更好地控制記憶體的使用。 對於處理大量影像的應用程式而言,AnyBitmap 的記憶體效率可大幅減少整體記憶體佔用量。 請參閱我們的 System.Drawing.Common Alternatives 指南,以瞭解移轉技巧。 如何指定掃描區域? 在OcrImageInput類別的建構中,您可以指定要掃描的區域。 這樣就可以定義影像文件中用於 OCR 的特定區域。 視影像文件而定,指定掃描區域可大幅提升效能。 在提供的程式碼範例中,只擷取了章節編號和標題。 :path=/static-assets/ocr/content-code-examples/how-to/input-images-read-specific-region.cs using IronOcr; using IronSoftware.Drawing; using System; // Instantiate IronTesseract IronTesseract ocrTesseract = new IronTesseract(); // Specify crop region Rectangle scanRegion = new Rectangle(800, 200, 900, 400); // Add image using var imageInput = new OcrImageInput("Potter.tiff", ContentArea: scanRegion); // Perform OCR OcrResult ocrResult = ocrTesseract.Read(imageInput); // Output the result to console Console.WriteLine(ocrResult.Text); Imports IronOcr Imports IronSoftware.Drawing Imports System ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Specify crop region Private scanRegion As New Rectangle(800, 200, 900, 400) ' Add image Private imageInput = New OcrImageInput("Potter.tiff", ContentArea:= scanRegion) ' Perform OCR Private ocrResult As OcrResult = ocrTesseract.Read(imageInput) ' Output the result to console Console.WriteLine(ocrResult.Text) $vbLabelText $csharpLabel 何時應該使用區域掃描以獲得更好的效能? 當您只需要一致文件佈局中特定區域的文字時,區域掃描可大幅提升效能。 常見的用例包括擷取標頭、表單欄位、發票總數或 ID 卡資訊。 對於文字只佔一小部分的大型圖片,效能提升最為顯著。 對於 3000x4000 像素的發票,僅掃描總金額區域的速度可比全頁 OCR 快 10-20 倍。 區域掃描也可消除其他區域可能產生的雜訊,從而提高精確度。 如需更多以區域為基礎的範例,請參閱我們的 Content Areas & Crop Regions with PDFs 指南。 如何確定我所在區域的正確座標? 確定座標需要瞭解 Rectangle 使用 (X, Y, Width, Height) 格式,其中 (0,0) 為左上角。 首先在可顯示游標座標的影像編輯器中開啟您的影像。 或者,使用 IronOCR 的調試功能,將偵測到的文字區域可視化。 對於動態佈局,可考慮先使用 IronOCR 執行全面掃描,再分析 OcrResult 以程式化方式尋找文字位置。 我們的 Highlight Texts for Debugging 範例展示了如何將 OCR 區域可視化,以進行精確的座標判定。 如果區域超出影像邊界會如何? 當指定的區域超出影像邊界時,IronOCR 會自動將其剪輯至有效的影像區域。 例如,如果您的圖片是 1000x1000 像素,而您指定一個矩形在 (900, 900, 200, 200),IronOCR 只會處理從 (900, 900) 到 (1000, 1000) 的區域。 此自動剪輯功能可避免錯誤,但如果您的座標不正確,可能會導致文字擷取不完整。 請務必根據實際影像尺寸來驗證您的區域。 對於動態影像大小,請以百分比而非固定像素計算區域。 OCR Region of an Image 指南提供了更多安全區域處理的範例。 OCR結果 常見問題解答 如何在 C# 中使用 OCR 從 System.Drawing.Bitmap 擷取文字? IronOCR 使從 System.Drawing.Bitmap 物件中提取文字變得簡單。首先,實體化 IronTesseract 類別,然後使用 using 語句將您的 Bitmap 包裝成 OcrImageInput 物件,最後再呼叫 Read 方法。最小的程式碼是: var result = new IronOcr.IronTesseract().Read(new IronOcr.OcrImageInput(bitmap)); 在使用 OcrImageInput 時,為什麼 using statement 非常重要? using 語句非常重要,因為 OcrImageInput 實作 IDisposable 並持有需要適當清理的非管理資源。沒有它,您就有記憶體洩漏或檔案鎖定的風險,尤其是在處理多個影像時。IronOCR 的 OcrImageInput 需要適當的處理,以確保您的 .NET 應用程式中有效率的資源管理。 我可以在 System.Drawing.Image 物件上執行 OCR 嗎? 是的,IronOcr 支援 System.Drawing.Image 物件上的 OCR,因為 Image 是 Bitmap 的基類。只需將您的 Image 物件包裝在 OcrImageInput 中,就像使用 Bitmap 的方式一樣,IronOCR 就能在 Windows、macOS 和 Linux 平台上無縫擷取文字。 什麼是 IronSoftware.Drawing.AnyBitmap,它與 OCR 有何關聯? IronSoftware.Drawing.AnyBitmap 是來自 IronDrawing 的位圖類,IronDrawing 是一個開放原始碼的函式庫,可協助取代 .NET 專案中的 System.Drawing.Common。它提供 Windows、macOS 和 Linux 的跨平台相容性,因此當您需要跨不同作業系統進行一致的影像處理時,非常適合與 IronOCR 搭配使用。 我可以指定影像的特定區域進行文字擷取嗎? 是的,IronOCR 允許您透過指定裁切區域來定義特定的閱讀區域。此功能可讓您將 OCR 處理集中在 System.Drawing 物件的特定區段,當您只需要影像特定部分的文字時,可提高效能與精確度。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 審核人 Jeffrey T. Fritz 首席計畫經理 - .NET 社群團隊 Jeff 也是 .NET 和 Visual Studio 團隊的首席計畫經理。他是 .NET Conf 虛擬會議系列的執行製作人,並主持「Fritz and Friends」開發人直播串流,每週播出兩次,與觀眾一起討論技術和編寫程式碼。Jeff 為 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit 等大型 Microsoft 開發人員活動撰寫工作坊、簡報和規劃內容。 準備好開始了嗎? Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:5,384,824 查看許可證