1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<%@ Page Language="C#" MasterPageFile="~/tpl/Demo.Master" AutoEventWireup="true" CodeBehind="LoadImage.aspx.cs" Inherits="Aspose.OCR.Demos.LoadImage" Title="Aspose.OCR Demos" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeaderContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <table width="90%" align="center" cellspacing="0" cellpadding="0" border="0"> <tr> <td height="41" valign="top"> <img src="/Common/images/heading_lft.jpg" width="19" height="41" /></td> <td width="100%" class="demos-heading-bg"> <h2 class="demos-heading-bg"> Extracting OCR text from a BMP/TIFF image file - Aspose.OCR for .NET</h2> </td> <td valign="top"> <img src="/Common/images/heading_rt.jpg" width="19" height="41" /></td> </tr> </table> Please browse a valid BMP or TIFF image file and press a button to check the output. <strong>Note</strong>: Arial and Times New Roman (16pt and 32 pt) fonts are supported. <br /> <br /> <br /> <br /> <table> <tr> <td style="width: 500px" align="left"> <asp:FileUpload ID="FileUpload1" runat="server" /></td> </tr> <tr> <td align="left" style="width: 500px"> <asp:CheckBox ID="chkRotation" runat="server" Text="Rotated Text" /> (Select this option if text in the image is rotated)</td> </tr> <tr> <td align="left"> <asp:Button ID="btnExtractText" runat="server" Text="Extract Text" OnClick="btnExtractText_Click" /> </td> </tr> <tr> <td align="left" style="height: 30px; color: red;"><%=Session["outMessage"] %> <% Session["outMessage"] = ""; %> </td> </tr> <tr> <td align="left"> Extracted OCR Text</td> </tr> <tr> <td align="left" style="width: 100px"> <asp:TextBox ID="txtExtractedText" runat="server" Height="200px" ReadOnly="True" TextMode="MultiLine" Width="500px"></asp:TextBox></td> </tr> </table> </asp:Content>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Aspose.OCR; using System.IO; namespace Aspose.OCR.Demos { public partial class LoadImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnExtractText_Click(object sender, EventArgs e) { const string resourceFileName = @"Path\2011.07.02 v1.0 Aspose.OCR.Resouces.zip"; /********** BMP/TIFF File Upload block [Starts]****************/ ///The file name to be read string uploadedImage = ""; ///Upload file to server if (this.FileUpload1.HasFile) { if (this.FileUpload1.FileName.EndsWith(".bmp") || this.FileUpload1.FileName.EndsWith(".tiff")) { uploadedImage = this.FileUpload1.FileName; if (!File.Exists(MapPath(uploadedImage))) { this.FileUpload1.PostedFile.SaveAs(MapPath(this.FileUpload1.FileName)); } } else { ///Write html message and exit Session["outMessage"] = "<h3>Invalid file</h3>"; return; } } else { //////Write html message and exit Session["outMessage"] = "<h3>Please upload BMP or TIFF file</h3>"; return; } /********** File Upload block [Ends]****************/ try { //Create OcrEngine instance and assign //image, language and image configuration OcrEngine ocr = new OcrEngine(); ocr.Image = ImageStream.FromFile(Server.MapPath(uploadedImage)); ocr.Languages.AddLanguage(Language.Load("english")); ocr.Config.NeedRotationCorrection = chkRotation.Checked; ocr.Config.UseDefaultDictionaries = true; //Set resource file name and extract OCR text using (ocr.Resource = new FileStream(resourceFileName, FileMode.Open)) { try { if (ocr.Process()) { txtExtractedText.Text = ocr.Text.ToString(); } } catch (Exception ex) { Session["outMessage"] = "Exception: " + ex.Message; } } ocr = null; } catch (Exception ex) { Session["outMessage"] = "Exception: " + ex.Message; } } } }