---
title: "Choosing a zone to OCR in C#"
canonical_url: "https://www.nutrient.io/guides/dotnet/samples/select-ocr-zone-cs/"
md_url: "https://www.nutrient.io/guides/dotnet/samples/select-ocr-zone-cs.md"
last_updated: "2026-05-21T17:12:02.215Z"
description: "Learn how to effectively choose OCR contexts for precise text recognition on forms, ensuring only relevant data is processed for optimal results."
---

# Choosing the appropriate OCR zone in C#

This example shows how to choose the appropriate OCR context. OCR context is easy to set and use, and it’s important in the OCR process. Often on your forms, you don’t want to recognize all the text in the document, but only small parts of it — whether it’s a column in a table, a name on a forum, or an additional notes area. You have to inform the OCR engine of the layout type of the data you want to process using OCR.

[Get Started](https://www.nutrient.io/sdk/dotnet/getting-started.md)

[All Samples](https://www.nutrient.io/guides/dotnet/samples.md)

[Download](https://www.nutrient.io/guides/dotnet/downloads.md)

[Launch Demo](https://www.nutrient.io/demo/)

---

### C#

[C#](https://www.nutrient.io/guides/dotnet/samples/select-ocr-zone-cs.md)

### VB.NET

[VB.NET](https://www.nutrient.io/guides/dotnet/samples/select-ocr-zone-vbnet.md)

```cs

//We assume that GdPicture has been correctly installed and unlocked.
//We assume a GdViewer object called GdViewer1 has been created and painted on the form.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
//Loading the image from a file.
int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("image.tif");
//Checking if the image has been loaded correctly.
if (oGdPictureImaging.GetStat()!= GdPictureStatus.OK)
{
   MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
   //Displaying the image in the GdViewer.
   GdViewer1.DisplayFromGdPictureImage(imageId);
   oGdPictureImaging.ReleaseGdPictureImage(imageId);
}
oGdPictureImaging.Dispose();

//On the Mouse UP event on the GdViewer, set the ROI based on the rectangle of selection of the GdViewer.
//If a rectangle of selection is painted on the GdViewer, the area will be OCRed with a single line OCR context.
public void OCR_ROI_MouseUP(System.Object eventSender, System.EventArgs eventArgs)
{
   GdPictureOCR oGdPictureOCR = new GdPictureOCR();
   //Initializing variables to hold the position of the rectangle of selection on the document.
   int leftArea = 0;
   int topArea = 0;
   int widthArea = 0;
   int heightArea = 0;
   //Checking if a rectangle of selection has been painted on the GdViewer.
   if (GdViewer1.IsRect())
   {
	  //Getting the location of the selection on the document.
	  GdViewer1.GetRectCoordinatesOnDocument(ref leftArea, ref topArea, ref widthArea, ref heightArea);
	  //Setting the ROI on the document.
	  oGdPictureOCR.SetROI(leftArea, topArea, widthArea, heightArea);
   }
   else
   {
	  oGdPictureOCR.ResetROI();
   }
   //Processing the OCR:
   //Selecting the OCR context, here we will use it to recognize a single line on our form.
   oGdPictureOCR.Context = OCRContext.OCRContextSingleLine;
   //Setting up the required parameters for the OCR engine.
   oGdPictureOCR.ResourceFolder = "Redist\\OCR";
   if ((oGdPictureOCR.AddLanguage(OCRLanguage.English) == GdPictureStatus.OK) &&
	   (oGdPictureOCR.SetImage(imageId) == GdPictureStatus.OK))
   {
	  //Running the OCR.
	  string resID = oGdPictureOCR.RunOCR();
	  //Checking the status.
	  if (oGdPictureOCR.GetStat()!= GdPictureStatus.OK)
	  {
		 MessageBox.Show("The OCR has failed with the status: " + oGdPictureOCR.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
	  }
	  else
	  {
		 //Checking the results.
		 for (int i = 1; i <= oGdPictureOCR.GetCharacterCount(resID); i++)
		 {
			oGdPictureImaging.DrawRectangle(imageId,
				oGdPictureOCR.GetCharacterLeft(resID, i) + leftArea,
				oGdPictureOCR.GetCharacterTop(resID, i) + topArea,
				oGdPictureOCR.GetCharacterRight(resID, i) - oGdPictureOCR.GetCharacterLeft(resID, i),
				oGdPictureOCR.GetCharacterBottom(resID, i) - oGdPictureOCR.GetCharacterTop(resID, i),
				1, Color.Red, false);

		 }
	  }
   }
   oGdPictureOCR.Dispose();
}

```

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.

---

## Related pages

- [Saving images into folders based on their barcode](/guides/dotnet/samples/barcode-auto-sorting-vbnet.md)
- [Automatically rotate pages of a multipage TIFF file using OCR and C#](/guides/dotnet/samples/auto-rotate-tiff-cs.md)
- [Generate a multipage TIFF file using a compression mode per page in C#](/guides/dotnet/samples/create-multipage-tiff-cs.md)
- [Automatically rotate pages of a multipage TIFF file using OCR and VB.NET](/guides/dotnet/samples/auto-rotate-tiff-vbnet.md)
- [Saving images into folders based on their barcode](/guides/dotnet/samples/barcode-auto-sorting-cs.md)
- [Converting a Microsoft Word binary file document (DOC) to a Microsoft Word OpenXML document (DOCX) in C#](/guides/dotnet/samples/doc-to-docx-cs.md)
- [Convert PDFs, MS Office, and images to TIFF in C#](/guides/dotnet/samples/convert-to-tiff-cs.md)
- [Using an external OCR engine during PDF/OCR generation](/guides/dotnet/samples/integrate-omnipage-ocr-cs.md)
- [Merging two PDF documents by alternating pages from both files in C#](/guides/dotnet/samples/merge-pdf-alternate-pages-cs.md)
- [Merging two PDF documents by alternating pages from both files in VB.NET](/guides/dotnet/samples/merge-pdf-alternate-pages-vbnet.md)
- [Recompress an existing PDF using color detection in C#](/guides/dotnet/samples/recompress-pdf-cs.md)
- [Converting a multipage PDF to a multipage TIFF using color detection in C#](/guides/dotnet/samples/pdf-to-tiff-color-detection-cs.md)
- [Converting a multipage PDF to a multipage TIFF using color detection in VB.NET](/guides/dotnet/samples/pdf-to-tiff-color-detection-vbnet.md)
- [Recompress existing PDFs using MRC compression in C#](/guides/dotnet/samples/recompress-pdf-mrc-cs.md)
- [Recompress existing PDFs using MRC compression in VB.NET](/guides/dotnet/samples/recompress-pdf-mrc-vbnet.md)
- [Highlight and crop an image in C#](/guides/dotnet/samples/select-image-crop-area-cs.md)
- [Recompress an existing PDF using color detection in VB.NET](/guides/dotnet/samples/recompress-pdf-vbnet.md)
- [Highlight and crop an image in VB.NET](/guides/dotnet/samples/select-image-crop-area-vbnet.md)
- [Highlight a region in a PDF or an image in C#](/guides/dotnet/samples/select-pdf-image-region-cs.md)
- [Highlight a region in PDF or an image in VB.NET](/guides/dotnet/samples/select-pdf-image-region-vbnet.md)
- [Save bitonal TIFFs with VB.NET using photometric 0](/guides/dotnet/samples/tiff-photometric-vbnet.md)
- [Save bitonal TIFF images with C# and CCITT4](/guides/dotnet/samples/tiff-photometric-cs.md)
- [Choosing the appropriate OCR zone in VB.NET](/guides/dotnet/samples/select-ocr-zone-vbnet.md)
- [Convert a multipage TIFF to a multipage JBIG2 in C#](/guides/dotnet/samples/tiff-to-jbig2-cs.md)
- [Convert a multipage TIFF to a multipage JBIG2 in VB.NET](/guides/dotnet/samples/tiff-to-jbig2-vbnet.md)

