1. Simplest code to OCR an image using Microsoft Office's Imaging functionality (requires MS-Office 2007 or later, imaging components must be installed and MODI must be added to references).

    private string OCR ( string fileToOCR)
    {
    MODI.Document md = new MODI.Document();

    md.Create(fileToOCR);
    md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
    MODI.Image img = (MODI.Image) md.Images[0];
    MODI.Layout layout = img.Layout;
    layout = img.Layout;
    string result = layout.Text;
    md.Close (false);

    return result;

    }

    Calling function can be:
    private void button6_Click(object sender, EventArgs e)
    {
    MessageBox.Show ( OCR ("C:\\temp\\in.tif"));
    }
    7

    View comments


  2. using ADOX; //Requires Microsoft ADO Ext. 2.8 for DDL and Security
    using ADODB;
    public bool CreateNewAccessDatabase(string fileName)
    {
        bool result = false;
        ADOX.Catalog cat = new ADOX.Catalog();
        ADOX.Table table = new ADOX.Table();
        //Create the table and it's fields.
        table.Name = "Table1";
        table.Columns.Append("Field1");
        table.Columns.Append("Field2");
        try
        {
            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5");
            cat.Tables.Append(table);
            //Now Close the database
            ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
            if (con != null)
            con.Close();
            result = true;
        }
        catch (Exception ex)
        {
            result = false;
        }
        cat = null;
        return result;
    }
    7

    View comments

  3. Use [DataBase]

    ALTER TABLE Schema.TableName
    ADD FieldName Bit NOT NULL DEFAULT 0 WITH VALUES
    0

    Add a comment

  4. public bool IsDate(object value)
    {
    bool result = false;
    string strDate = value.ToString();

    try
    {
    DateTime dt = DateTime.Parse(strDate);
    if(dt != DateTime.MinValue && dt != DateTime.MaxValue)
    result = true;
    }
    catch (Exception ex)
    {
    result = false;
    }

    return result;
    }
    0

    Add a comment

  5. public bool IsNumeric(string value)
    {
    bool result = false;

    try
    {
    Convert.ToInt64(value);
    result = true;
    }
    catch (Exception ex)
    {
    result = false;
    }
    return result;
    }
    0

    Add a comment

  6. To implement VB like Input Box in C# you may use the following code:

    ________________________________________________________________________

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace zSharp.Util
    {
    public partial class InputBox : Form
    {
    public static DialogResult Show(string Prompt, string Title, string Default, int XPos, int YPos, ref string value)
    {
    //InitializeComponent();

    Form form = new Form();
    Label label = new Label();
    TextBox textBox = new TextBox();
    Button buttonOk = new Button();
    Button buttonCancel = new Button();

    form.Text = Title;
    label.Text = Prompt;
    textBox.Text = Default;


    buttonOk.Text = "OK";
    buttonCancel.Text = "Cancel";
    buttonOk.DialogResult = DialogResult.OK;
    buttonCancel.DialogResult = DialogResult.Cancel;

    label.SetBounds(9, 20, 372, 13);
    textBox.SetBounds(12, 36, 372, 20);
    buttonOk.SetBounds(228, 72, 75, 23);
    buttonCancel.SetBounds(309, 72, 75, 23);

    label.AutoSize = true;
    textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
    buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

    form.ClientSize = new Size(396, 107);
    form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
    form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
    form.FormBorderStyle = FormBorderStyle.FixedDialog;
    form.StartPosition = FormStartPosition.CenterScreen;
    form.MinimizeBox = false;
    form.MaximizeBox = false;
    form.AcceptButton = buttonOk;
    form.CancelButton = buttonCancel;

    if ((XPos > 0) || (YPos > 0))
    {
    form.StartPosition = FormStartPosition.Manual;
    form.Left = XPos;
    form.Top = YPos;
    }

    DialogResult dialogResult = form.ShowDialog();
    value = textBox.Text;
    return dialogResult;
    }

    }

    }
    _____________________________________________________________________

    To call the function use:

    string result = string.Empty;
    if ((zSharp.Util.InputBox.Show( "Enter A Value", this.Text, "First", 500, 500,ref result)) == DialogResult.OK)
    {
    MessageBox.Show(result);
    }
    1

    View comments

  7. For a project I needed to figure the dates of first and last day of the week, month, quarter
    and year for a given date.


    The first thing we need to do is define an Enum of Frequency types

    public enum FrequencyType
    {
    None = 0,
    Daily = 1,
    Weekly = 2,
    Monthly = 3,
    Quarterly = 4,
    Annually = 5,
    }


    The actual procedure get the target date and frequency type, it returns an array of string containing the first and last date. The procedure returns an array of string that contains the first date and the last date of the given frequency.

    private string[] GetRange(FrequencyType frequency, DateTime dateToCheck)
    {
    string[] result = new string [2];
    DateTime dateRangeBegin = dateToCheck;
    TimeSpan duration = new TimeSpan(0, 0, 0, 0); //One day
    DateTime dateRangeEnd = DateTime.Today.Add(duration);

    switch (frequency)
    {
    case FrequencyType.Daily:
    dateRangeBegin = dateToCheck;
    dateRangeEnd = dateRangeBegin;
    break;

    case FrequencyType.Weekly:
    dateRangeBegin = dateToCheck.AddDays(-(int)dateToCheck.DayOfWeek);
    dateRangeEnd = dateToCheck.AddDays(6 - (int)dateToCheck.DayOfWeek);
    break;

    case FrequencyType.Monthly:
    duration = new TimeSpan(DateTime.DaysInMonth ( dateToCheck.Year, dateToCheck.Month) - 1 , 0, 0, 0);
    dateRangeBegin = dateToCheck.AddDays((-1) * dateToCheck.Day + 1);
    dateRangeEnd = dateRangeBegin.Add(duration);
    break;

    case FrequencyType.Quarterly:
    int currentQuater = (dateToCheck.Date.Month - 1) / 3 + 1;
    int daysInLastMonthOfQuarter = DateTime.DaysInMonth(dateToCheck.Year, 3 * currentQuater );
    dateRangeBegin = new DateTime ( dateToCheck.Year, 3 * currentQuater - 2, 1);
    dateRangeEnd = new DateTime(dateToCheck.Year, 3 * currentQuater , daysInLastMonthOfQuarter);
    break;

    case FrequencyType.Annually:
    dateRangeBegin = new DateTime(dateToCheck.Year, 1, 1);
    dateRangeEnd = new DateTime(dateToCheck.Year, 12, 31);
    break;
    }
    result[0] = dateRangeBegin.Date.ToString();
    result[1] = dateRangeEnd.Date.ToString();
    return result;
    }


    To test the function we can write a test method.

    //Test Data
    private void GetDateRanges()
    {
    string[] dates;
    StringBuilder result = new StringBuilder("");

    dates = this.GetRange(FrequencyType.Daily, DateTime.Today); //Or any any other date you want to test.
    result.AppendLine(string.Format("Daily Range : {0} - {1}", dates[0], dates[1]));

    dates = this.GetRange(FrequencyType.Weekly, DateTime.Today);
    result.AppendLine(string.Format("Weekly Range : {0} - {1}", dates[0], dates[1]));

    dates = this.GetRange(FrequencyType.Monthly, DateTime.Today);
    result.AppendLine(string.Format("Monthly Range : {0} - {1}", dates[0], dates[1]));

    dates = this.GetRange(FrequencyType.Quarterly, DateTime.Today);
    result.AppendLine(string.Format("Quarterly Range : {0} - {1}", dates[0], dates[1]));

    dates = this.GetRange(FrequencyType.Annually, DateTime.Today);
    result.AppendLine(string.Format("Annually Range : {0} - {1}", dates[0], dates[1]));

    MessageBox.Show(result.ToString());

    }
    2

    View comments

Blog Archive
Topics
Topics
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.