Friday, June 26, 2009

Office 2007 OCR Sample Code Using C#

this sample code for:

1- scan image Format in Specify Directory.
2- read text from these images.
3- save text from each image in text fle automaticly.
4- handle problems with images

Sample Code:

public void CheckFileType(string directoryPath)
{
IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator();
while (files.MoveNext())
{
//get file extension
string fileExtension = Path.GetExtension(Convert.ToString(files.Current));

//get file name without extenstion

string fileName=Convert.ToString(files.Current).Replace(fileExtension,string.Empty);

//Check for JPG File Format

if (fileExtension == ".jpg")
{
try
{
//OCR Operations ...
MODI.Document md = new MODI.Document();
md.Create(Convert.ToString(files.Current));
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image image = (MODI.Image)md.Images[0];

//create text file with the same Image file name

FileStream createFile = new FileStream(fileName + ".txt",FileMode.CreateNew);

//save the image text in the text file

StreamWriter writeFile = new StreamWriter(createFile);
writeFile.Write(image.Layout.Text);
writeFile.Close();
}
catch (Exception)
{
//MessageBox.Show("This Image hasn't a text or has a problem",
"OCR Notifications",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

Tuesday, June 23, 2009

How To: Create Thumbnail Images in ASP.NET

A thumbnail image is a small version of an image. You can create a thumbnail image by the following Methods:

We will Use: System.Drawing.Imaging

 

Copy the Following Code into your Class:

 

public void GenerateThumbnail(string thumbPath, int thumbWidth, int thumbHeight, string thumbNewPath)

        {

            String imageName = Path.GetFileName(thumbPath);

 

            int imageHeight = thumbHeight;

            int imageWidth = thumbWidth;

 

            Image fullSizeImg = Image.FromFile(thumbPath);

            Image.GetThumbnailImageAbort dummyCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallback);

            Image thumbNailImage = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);

 

            thumbNailImage.Save(thumbNewPath, ImageFormat.Jpeg);

            thumbNailImage.Dispose();

            fullSizeImg.Dispose();

 

        }

 

        public bool ThumbnailCallback()

        {

            return false;


        }

I think the following Code is better or I can say it’s a good sample for optimizing the above code.


public void GenerateThumbnail (string thumbPath, int thumbWidth, int thumbHeight, string thumbNewPath)

        {

            Image image = new Bitmap(thumbPath);

            Image imageThumbnail = image.GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr());

            imageThumbnail.Save(thumbNewPath);


        }

How To: Use Active Directory Membership Provider in ASP.NET

The ASP.NET version 2.0 membership feature provides secure credential storage for application users.

We Will Use The Following:

- Web Page Named Login.aspx and another Web Page Named CreateUser.aspx

- Login Control

- Create User Wizard Control

Steps:

1- Configure Forms Authenticationin Web.config File


To configure forms authentication, set the <authentication> element's mode attribute to "Forms" and then configure your application's Web.config file as shown in the following example.





<authentication mode="Forms">
    <forms loginUrl="Login.aspx" 

           protection="All" 

           timeout="30" 

           name="AppNameCookie" 

           path="/FormsAuth" 

           requireSSL="false" 

           slidingExpiration="true" 

           defaultUrl="default.aspx"

           cookieless="UseCookies"

           enableCrossAppRedirects="false"/>

</authentication>

Where:


  • loginUrl points to the login page. You should place this in a folder that requires Secure Sockets Layer (SSL) for access.

  • protection is set to "All" to specify privacy and integrity for the forms authentication ticket.

  • timeout is used to specify a limited session lifetime.

  • name and path are set to unique values for the current application.

  • requireSSL is set to "false". This configuration means that authentication cookie can be transmitted over channels that are not SSL-protected. If you are concerned about session hijacking, you should consider setting this to "true".

  • slidingExpiration is set to "true" to enforce a sliding session lifetime. This means that the timeout is reset after each request to your application.

  • defaultUrl is set to the Default.aspx page for the application.

  • cookieless is set to "UseCookies" to specify that the application uses cookies to send the authentication ticket to the client.

  • enableCrossAppRedirects is set to "false" to indicate that the application cannot redirect requests outside the application scope.


Add the following <authorization> element after the <authentication> element. This permits only authenticated users to access the application. The previously established loginUrl attribute of the <authentication> element will redirect unauthenticated requests to the Login.aspx page.




<authorization>

<deny users="?" />

<allow users="*" />

</authorization>


Configure the ActiveDirectoryMembershipProvider in Web.config File

Configure the ActiveDirectoryMembershipProvider in your application's Web.config file as shown in the following example.

There is An Important Point in this Case How I Can Get My Active Directory Connection String

Please Visit:


<connectionStrings>

<add name="ADConnectionString"

connectionString=

"LDAP://domain.testing.com/CN=Users,DC=domain,DC=testing,DC=com" />

</connectionStrings>


<system.web>

...

<membership defaultProvider="MembershipADProvider">

<providers>

<add name="MembershipADProvider"

type="System.Web.Security.ActiveDirectoryMembershipProvider,System.Web,

Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

connectionStringName="ADConnectionString"

connectionUsername="<domainName>\administrator"

connectionPassword="password"/>

</providers>

</membership>

...

</system.web>

The Last Step:

-      Drag and drop Login Control into the Login page.

-      Drag and drop Create User Wizard Control into the CreateUser page.

Now Your Web Application or your Website Secured.

How To: Use SQL Membership Provider in ASP.NET

The ASP.NET version 2.0 membership feature provides secure credential storage for application users.

We Will Use The Following:

- Web Page Named Login.aspx and another Web Page Named CreateUser.aspx

- Login Control

- Create User Wizard Control

Steps:

1- Configure Forms Authentication in Web.config File


To configure forms authentication, set the <authentication> element's mode attribute to "Forms" and then configure your application's Web.config file as shown in the following example.




<authentication mode="Forms">

<forms loginUrl="Login.aspx"

protection="All"

timeout="30"

name="AppNameCookie"

path="/FormsAuth"

requireSSL="false"

slidingExpiration="true"

defaultUrl="default.aspx"

cookieless="UseCookies"

enableCrossAppRedirects="false" />

</authentication>


  • loginUrl points to the login page. You should place this in a folder that requires Secure Sockets Layer (SSL) for access.

  • protection is set to "All" to specify privacy and integrity for the forms authentication ticket.

  • timeout is used to specify a limited session lifetime.

  • name and path are set to unique values for the current application.

  • requireSSL is set to "false". This configuration means that authentication cookie can be transmitted over channels that are not SSL-protected. If you are concerned with session hijacking, you should consider setting this to "true".

  • slidingExpiration is set to "true" to enforce a sliding session lifetime. This means that the timeout is reset after each request to your application.

  • defaultUrl is set to the Default.aspx page for the application.

  • cookieless is set to "UseCookies" to specify that the application uses cookies to send the authentication ticket to the client.

  • enableCrossAppRedirects is set to "false", to indicate that the application cannot redirect the request outside the application scope.



Add the following <authorization> element after the <authentication> element. This permits only authenticated users to access the application. The previously established loginUrl attribute of the <authentication> element redirects unauthenticated requests to the Login.aspx page



<authorization>
   <deny users="?" />

   <allow users="*" />

 </authorization>

2- Install the Membership Database

Before you can use the SqlMembershipProvider, you must install the SQL Server membership database.

To install the membership database, log on to your server with an account that has authority to administrate SQL Server (such as the Administrator account). Open the Visual Studio 2005 command prompt (Start > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 command prompt), and run the following command:

aspnet_regsql.exe -E -S localhost -A m

Where:


  • -E indicates authenticate using the Windows credentials of the currently logged on user.

  • -S (server) indicates the name of the server where the database will be installed or is already installed.

  • -A m indicates add membership support. This creates the tables and stored procedures required by the membership provider.


In Web.config

<connectionStrings>

 <add name="MySqlConnection" connectionString="Data Source=MySqlServer;Initial Catalog=aspnetdb;Integrated Security=SSPI;" />

</connectionStrings>

<system.web>

...

 <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">

    <providers>

      <clear />

      <add 

        name="SqlProvider" 

        type="System.Web.Security.SqlMembershipProvider" 

        connectionStringName="MySqlConnection"

        applicationName="MyApplication"

        enablePasswordRetrieval="false"

        enablePasswordReset="true"

        requiresQuestionAndAnswer="true"

        requiresUniqueEmail="true"

        passwordFormat="Hashed" />

    </providers>

 </membership>

The Last Step:

-      Drag and drop Login Control into the Login page

-      Drag and drop Create User Wizard Control into the CreateUser page

Now Your Web Application or your Website Secured.

Saturday, June 6, 2009

How To: Format DateTime in SQL Server

I'll Show You How To Format DataTime in SQL SERVER.


When expression is a date or time data type, style can be one of the values shown in the below Text:

  • 101 >>>>>>>> mm/dd/yy
  • 102 >>>>>>>> yy.mm.dd
  • 103 >>>>>>>> dd/mm/yy
  • 104 >>>>>>>> dd.mm.yy
  • 111 >>>>>>>> yy/mm/dd
  • 114 >>>>>>>> hh:mi:ss:mmm(24h)

more formats

we will assume we have a table contain a DateTime data type field, the Result of the below query will be like:

ex. SELECT BirthDate FROM MyTable

the Result will be ("2/2/1982 1:00:00 AM")

so we can use use Convert Function To Format DateTime

ex. SELECT CONVERT(CHAR(11),GETDATE(),101) BirthDate FROM MyTable

{Result === 06/07/2009 }

ex. SELECT CONVERT(CHAR(11),GETDATE(),114) BirthDate FROM MyTable

{Result === 1:00:00 }