Maen,
Thanks for the response. I have a user control with a panel control on that. I dynamically create the OcrRasterImageViewer()control and add it to the panel control. The following code in the user control load event is where I set everything up:
private void OCRDemoIndexView_Load(object sender, EventArgs e)
{
try
{
// Set internal variables
mainWorkItem = (MainWorkItem)parentWorkItem;
btConfigHashTable = mainWorkItem.BTConfigHashTable;
btiSeriesDataConn = mainWorkItem.BTISeriesDataConn;
btISeriesApp.BTISeriesDataConn = btiSeriesDataConn;
btISeriesApp.BTDefaultServer = (String)mainWorkItem.BTConfigHashTable["BTSettingsDefaultServer"];
// Initialize Data Entry Fields for fresh start
BTOCRDemoRestart();
Messager.Caption = "BrownTech Optical Character Recognition Demo";
Text = Messager.Caption;
_xRes = 300;
_yRes = 300;
Support.Unlock(false);
RasterCodecs.Startup();
_codecs = new RasterCodecs();
_ocrStarted = false;
_pageAdded = false;
_pageRecognized = false;
_charsUpdated = false;
_viewer = new OcrRasterImageViewer();
_viewer.BackColor = Color.AntiqueWhite;
_viewer.Dock = DockStyle.Fill;
_viewer.AutoScroll = true;
_viewer.DoubleBuffer = true;
panel1.Controls.Add(_viewer);
_viewer.BringToFront();
_viewer.MouseDraw = false;
using ( WaitCursor wait = new WaitCursor())
{
if (RasterSupport.IsLocked(RasterSupportType.Ocr))
{
Messager.ShowError(this, "You need to unlock the OCR engine before you can use this demo!");
return;
}
_rasterEngine = RasterDocumentEngine.Instance;
try
{
_rasterEngine.Startup();
_rasterLanguage[0] = RasterDocumentLanguage.English;
_rasterEngine.SelectLanguages(_rasterLanguage);
}
catch (Exception ex)
{
// check if the OCR engine could be initialized
bool showEngine = false;
if (ex is RasterDocumentException)
{
RasterDocumentException oe = ex as RasterDocumentException;
if (oe.Code == RasterDocumentExceptionCode.InitializeEngine)
showEngine = true;
}
if (showEngine)
new EngineDialog().ShowDialog(this);
else
MessageBox.Show(ex.Message);
return;
}
try
{
RasterDocumentDrawZoneOptions drawOpts = _rasterEngine.DrawZoneOptions;
// Set Zone Drawing Options and Colors
_zonePen = new Pen(Color.Blue, 2);
_zonePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
drawOpts.Pen = _zonePen;
_selzonePen = new Pen(Color.Blue, 2);
_selzonePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
drawOpts.SelectedPen = _selzonePen;
_rasterEngine.DrawZoneOptions = drawOpts;
_viewer.RasterEngine = _rasterEngine;
_ocrStarted = true;
}
catch (Exception ex)
{
Messager.ShowError(this, ex.Message);
}
_imagesKeyValue = DemosGlobal.ImagesFolder;
}
}
catch (Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Global Policy");
if (rethrow)
{
throw;
}
}
}
The user will open an image using a command button and I setup up basic OCR capability as follows:
loader.LoadOnlyOnePage = true;
if (loader.Load(this, _codecs, true))
{
_viewer.OcrMode = false;
_viewer.DrawPage = false;
if (_viewer.Image != null)
_viewer.Image.Dispose();
ResetActivePage();
_viewer.Image = loader.Image;
if (_viewer.Image != null)
// Check to see if OCR was successfully started
if (_ocrStarted)
{
// Add Page to OCR Engine for Processing
_viewer.RasterEngine.AddPage(_viewer.Image, -1);
_pageAdded = true;
UpdateView();
//RefreshPictureBox();
// Prep Annotations
_viewer.AnnContainerObject = new AnnContainer();
_viewer.AnnContainerObject.Bounds = new AnnRectangle(0, 0, _viewer.ImageSize.Width, _viewer.ImageSize.Height, AnnUnit.Pixel);
_viewer.AnnContainerObject.Name = "Container";
_viewer.AnnContainerObject.Visible = true;
_viewer.AnnContainerObject.UnitConverter = new AnnUnitConverter(96, 96);
// Enable recongnize button
ultraButtonRecognize.Enabled = true;
return true;
}
else
{
return false;
}
else
{
return false;
}
}
Finally, I have a command button that I use to recognize a user enterable string and then hilite all the hits on the iamge. Its here that I add the annotations to the container.
private void ultraButtonRecognize_Click(object sender, EventArgs e)
{
// Declare local variables
Int32 iRecogWordCount = 0;
String sRDFFileName = "";
List alAnnotations = new List();
// Prompt for a file name and path to store the RDF File
// sRDFFileName = DemosGlobal.GetOcrOutputFileName(this, "RDF files (*.rdf)|*.rdf");
sRDFFileName = "c:\\dude.rdf";
if (sRDFFileName == "")
{
return;
}
// Initialize OCR Settings
_viewer.RasterEngine.EnableZoneForceSingleColumn = true;
_viewer.RasterEngine.ShowZoneGridlines = true;
_viewer.RasterEngine.FindZones(0, Rectangle.Empty);
_viewer.RasterEngine.SpellLanguageId = RasterDocumentLanguage.English;
_viewer.RasterEngine.EnableSubsystem = true;
_viewer.RasterEngine.EnableCorrection = true;
_viewer.RasterEngine.RecognitionDataFileName = sRDFFileName;
// Delete recognition file if it exists so we can reuse it.
if (File.Exists(sRDFFileName))
{
File.Delete(sRDFFileName);
}
// Recognize the first page in the document
_viewer.RasterEngine.Recognize(0, 1, null);
// Get list of recongized words for the first page
IList liRasterOcrRecognizedWords = _viewer.RasterEngine.GetRecognizedWords(0);
for (int i = 0; i 0)
{
// Load up an array of AnnHiliteObjects
for (int k = 0; k < iRecogWordCount; k++)
{
alAnnotations.Add(new AnnHiliteObject());
}
iRecogWordCount = 0;
// Highlight each recognized word
for (int j = 0; j < liRasterOcrRecognizedWords.Count - 1; j++)
{
if (liRasterOcrRecognizedWords[j].Word == ultraTextEditorOCRSearchString.Text)
{
alAnnotations[iRecogWordCount].Bounds = new AnnRectangle(liRasterOcrRecognizedWords[j].WordArea.Left, liRasterOcrRecognizedWords[j].WordArea.Top, liRasterOcrRecognizedWords[j].WordArea.Width, liRasterOcrRecognizedWords[j].WordArea.Height, AnnUnit.Pixel);
alAnnotations[iRecogWordCount].HiliteColor = Color.Yellow;
_viewer.AnnContainerObject.Objects.Add((AnnHiliteObject)alAnnotations[iRecogWordCount]);
_viewer.Refresh();
iRecogWordCount++;
}
}
// Test Annotation
// create Annotation Note Object and add it to the container
// annContainerObj.Objects.Add(CreateAnnNoteObject(new AnnRectangle(annContainerObj.Bounds.Right / 2, annContainerObj.Bounds.Bottom / 2, (annContainerObj.Bounds.Right / 4) - 1, (annContainerObj.Bounds.Bottom / 4) - 1)));
}
else
{
BTUtilities.BTMessageBox("The entered OCR Search String was not found on this page of the document.", MessageBoxButtons.OK);
ultraTextEditorOCRSearchString.Focus();
}
}
I have overridden the OnPostImagePaint and OnTransformChanged events in the OCRRasterImageViewer control as follows:
protected override void OnPostImagePaint(PaintEventArgs e)
{
if (annContainerObj != null)
annContainerObj.Draw(e.Graphics);
}
protected override void OnTransformChanged(EventArgs e)
{
if (annContainerObj != null)
annContainerObj.Transform.Clone();
}
Thanks
Beckstev
protected override void OnPostImagePaint(PaintEventArgs e)
{
if (annContainerObj != null)
annContainerObj.Draw(e.Graphics);
}
protected override void OnTransformChanged(EventArgs e)
{
if (annContainerObj != null)
annContainerObj.Transform.Clone();
}