Monday 15 December 2014

Converting Text to Voice in .NET Using C#

Introduction

The demo below explains how to convert text to speech using C# in Visual Studio 2010 using System.SpeechLibrary.
Microsoft .NET framework provides System.Speech.Synthesis for voice synthesis.

Building the Sample
You must have Visual Studio 2010 to build and run the sample.
Description

To convert text to speech which is called voice synthesis, you must include "System.Speech" reference in your project. It contains functions for both speech synthesis and recognition.
After adding System.speech reference, you have to use System.Speech.Synthesis in your project which contains various functions for voice synthesis.

Creating Object of SpeechSynthesizer Class

The first step is to create an object of SpeechSynthesizer class (e.g. "reader" in my sample).

SpeechSynthesizer reader = new SpeechSynthesizer();

Calling Speak() Function

The next step is to call the "Speak()" function by passing the text to spoken as string.

reader.Speak("This is my first speech project");

The problem with the speak() function is that it is not threaded. It means that you cannot perform any other function in your Windows Form until the "reader" object has completed the speech.
So it is better to use "SpeakAsync()" function. I have also used SpeakAsync() in the sample.

reader.SpeakAsync("Speaking text asynchronously");

Using Pause() and Resume() functions

You can also detect the state of the "reader" object by using "SynthesizerState" property. And using that, you can also "Pause" or "Resume" the narration.

if (reader.State == SynthesizerState.Speaking)
   {
                    reader.Pause();
   }

if (reader.State == SynthesizerState.Paused)
   {
                    reader.Resume();
  }

Using Dispose() function

You can use Dispose() function to stop narration and dispose the "reader" object.

if( reader!= null )
{
     reader.Dispose();
}


These are some of the basic operations. You can also change voice, volume, rate and other parameters. You can also save the spoken audio stream directly into a "wave" file.
The sample also shows some other features such as using event handlers to detect speech progress and display status of synthesizer.
Below is the complete code for speech synthesis.

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;
using System.Speech.Synthesis;
using System.IO;

namespace text_to_speech
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer reader; //declare the object
        public Form1()
        {
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            reader = new SpeechSynthesizer(); //create new object
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            textBox1.ScrollBars = ScrollBars.Both;
        }

        //SPEAK TEXT
        private void button1_Click(object sender, EventArgs e)
        {
            reader.Dispose();
            if (textBox1.Text != "")    //if text area is not empty
            {

                reader = new SpeechSynthesizer();
                reader.SpeakAsync(textBox1.Text);
                label2.Text = "SPEAKING";
                button2.Enabled = true;
                button4.Enabled = true;
                reader.SpeakCompleted += new EventHandler<speakcompletedeventargs />(reader_SpeakCompleted);
            }
            else
            {
                MessageBox.Show("Please enter some text in the textbox",
                 "Message", MessageBoxButtons.OK);
            }
        }

        //event handler
        void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            label2.Text = "IDLE";
        }

        //PAUSE
        private void button2_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Speaking)
                {
                    reader.Pause();
                    label2.Text = "PAUSED";
                    button3.Enabled = true;

                }
            }
        }

        //RESUME
        private void button3_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Paused)
                {
                    reader.Resume();
                    label2.Text = "SPEAKING";
                }
                button3.Enabled = false;
            }
        }

        //STOP
        private void button4_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                label2.Text = "IDLE";
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
            }
        }

        //load data from text file button
        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //copy data from text file and load it to textbox
            textBox1.Text =  File.ReadAllText(openFileDialog1.FileName.ToString());

        }

     }
}

Adding SYSTEM.SPEECH Reference



Thursday 20 November 2014

Boxing & UnBoxing In C#

C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. In this article I will share about understanding Boxing and UnBoxing in C#.
C# Type System contains three Types , they are Value Types , Reference Types and Pointer Types. C# allows us to convert a Value Type to a Reference Type, and back again to Value Types . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation

Wednesday 19 November 2014

View State In Asp.Net

  1. View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during post back are serialized into base64-encoded strings.
  2. When an ASP.NET page is submitted to the server, the state of the controls is encoded and sent to the server at every form submission in a hidden field known as

Tuesday 18 November 2014

What is Session State In Asp.Net

  1. ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request.Session is defined as the period of time that a unique user interacts with a Web application. When a new user begins to interact with the application, a new session ID is generated and associated with all subsequent requests from that same client and stored in a cookie on the client machine.

Caching In Asp.Net

Caching is the ability to store page output and data in memory for the first time it is requested and later we can quickly retrieve them for multiple, repetitious client requests. It is like keeping a copy of something for later use, so we can reduce the load factor on Web servers and database servers by implementing caching on a Web application.
Writing Code In C#
Cache.Add("dataset", dataset, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 60), System.Web.Caching.CacheItemPriority.Default, null);

How To Use Cookies ASP.NET.

Cookies provide the ability to store small amounts of data on a clients machine. It is often used to store user preferences, session variables, or identity. When the user visits your Web site another time, the application can retrieve the information it stored earlier.
The browser is responsible for managing cookies on a user system. Cookies can be created, accessed, and modified directly by script running on the client and pass between the client and server during requests.
Writing code in C#
Response.Cookies["UserName"].Value = "Ashok";
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);

How To Use Grid View Page Index Changing In Asp.Net

Here I Will Explain How to use Page Index/Pagination In Grid View.With various types of paging in ASP.NET. We use C# code to bind the SQL data with a Grid View control and use the following simple steps to make your ASP.NET Grid View control with paging enabled. The Grid View control provides you with an easy way to display the number of items on a page

What is Page Index In Asp.Net?
To show he number of items on a page in Grid View.

C# code to bind the SQL data with a Grid View control and use the following steps.
Create a SQL table

Saturday 1 November 2014

How To Copy Whole Database From One Computer To Another By Using Generate Script

What is Generate Script?
     This is a process in which we can generate the script of the existing database. And can use easily on other client system without loosing any Data.

Why it is different from import&export?
      By using Generate Script we can export whole database as well as data and script but in import & export  only we can extract the skeleton of the Database.It's a easiest way to copy database from one system to another.
 Here Are the steps to Generate Script of the database.
Step-1.
Right click on the database in sql and choose Task > Generate Script.

Tuesday 28 October 2014

What Is CMS ?

What Is  CMS ? CMS Stand for (Content management system) is a system used to manage the content of a Web site. that allows publishing, editing and modifying content, organizing, deleting as well as maintenance from a central interface. CMSs have been available since the late 1990s.
CMSs are often used to run websites containing blog, news, and shopping. Many corporate and marketing websites use CMSs.

Tuesday 21 October 2014

Freeze GridView Header Row In Asp.Net

In Grid View Control You Can easily display all the collection of data and we can easily add sorting and paging, and perform in-line editing.Some time there is large no of data which makes grid view scrollable, headers will also scroll along with the other grid view contents making it difficult for the user to understand the data properly.In this situations we can freeze the header row then the user can scroll the data and see the header as fixed.

Monday 20 October 2014

How to add Data Item Index in Grid View In Asp.Net

The GridView control is the successor to the DataGrid and extends it in a number of ways. While developing GridView control in ASP.NET, programmers often requires to display row number in GridView controls.
This can be accomplished by addingtthe Container.DataItemIndex  in the html markup of the Gridview control.

Thursday 16 October 2014

How To Add Dynamic Controls Into Gridview in Asp.net

How to add dynamic controls to the Grid view in the Code Behind page. when the page is refresh or the page is Post Back the dynamically created controls will disappear from Grid View, this sample will show how to resolve this issue.

Step 1. Create a C# "ASP.NET Web Application" Name it Add_Dynamic_Control_to_Grid-view".
Step 2.  Add a Grid-view Control to Default.aspx page then rename it to "MyData". This page will bind the data and show the dynamically created Link Button Control.

Thursday 25 September 2014

Monday 22 September 2014

How to Preview Uploaded Image before Upload To Server

Here i will explain how to preview the image file before uploaded to server. When we upload any image file to server or to any file location.It is not possible to find out the write selection you have choose which u want to upload to server. By this example we are able to preview the selected image file before upload and choose the correct selection which u want to upload.

Saturday 20 September 2014

What Is Delegate

Here I will explain what is Delegate and types of Delegate.
  1. Single Cast
  2. Multi Cast
  3. Asynchronous
Delegate is a type known as pointer type looks like method and perform like type derived from super type delegate.Delegate is Reliable declaring N number methods with same nature with different different operation can perform nay effective order through delegateDelegates can be various type

Friday 19 September 2014

Thursday 18 September 2014

Saturday 13 September 2014

Tuesday 9 September 2014

Thursday 4 September 2014

How To Bounce Left a div by Using jQuery plugin And use in Asp.Net.


Here i will explain how to move or bounce a particular division by using jquery.
Here i will use .velocity() function instead of $.animate() animation engine.
Velocity is a  animation engine that re-implements jQuery's $.animate() for better performance (making Velocity also faster than CSS animation libraries) while including new features to improve workflow.

  • Download Velocity include it on your page, and replace all  instances of jQuery's $.animate() with $.velocity(). You will immediately see a performance boost across all browsers and devices — especially on mobile.

Saturday 30 August 2014

File Upload By JQuery

How to start uploads with a button click

 we can start uploads files on the click of a button 

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            data.context = $('<button/>').text('Upload')
                .appendTo(document.body)
                .click(function () {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));
                    data.submit();
                });
        },
        done: function (e, data) {
            data.context.text('Upload finished.');
        }
    });
});

3 Tire Architecture.

3-Tier architecture is a very well know buzz word in the world of software development whether it web based or desktop based. In this article I am going to show how to design a web application based on 3-tier architecture.
Introduction 
3-Tier architecture generally contains UI or Presentation Layer, Business Access Layer (BAL) or Business Logic Layer and Data Access Layer (DAL). 

Presentation Layer (UI) 
Presentation layer cotains pages like .aspx or windows form where data is presented to the user or input is taken from the user. 

Business Access Layer (BAL) or Business Logic Layer 
BAL contains business logic, validations or calculations related with the data, if needed. I will call it Business Access Layer in my demo. 

Data Access Layer (DAL) 
DAL contains methods that helps business layer to connect the data and perform required action, might be returning data or manipulating data (insert, update, delete etc). For this demo application, I have taken a very simple example. I am assuming that I have to play with record of persons (FirstName, LastName, Age) and I will refer only these data through out this article.
 
What is Tier and Layer ?
Here I Will explain what is Tire  and Layer we need to clarify the difference between two terms in N-Tier architecture  Tier and Layer. Tier usually means the physical deployment computer. Usually an individual running server is one tier. Several servers may also be counted as one tier, such as server fail-over clustering. By contrast, layer usually means logic software component group mainly by functionality  layer is used for software development purpose. Layer software implementation has many advantages and is a good way to achieve N-Tier architecture. Layer and tier may or may not exactly match each other. Each layer may run in an individual tier. However, multiple layers may also be able to run in one tier. 

Tire:
It indicates a physical separation of components, which may mean different assemblies such as DLL, EXE, etc. 
Layer:
It indicates a physical separation of components, which may mean different assemblies such as DLL, EXE, etc. on the same server or multiple servers.

By using 3-Tier architecture in your project you can achive 

1. Seperation - the functionality is seperated from the data access and presentation so that it is more maintainable 
2. Independence - layers are established so that if one is modified (to some extent) it will not affect other layers. 
3. Reusability - As the layers are seperated, it can exist as a module that can be reused by other application by referencing it. 

Hope this article helped you understanding 3-Tier architecture and desiging it.

Friday 29 August 2014

How To Generate Dynamic Controls in Asp.net.

Introduction:
Here I will explain how to generate a controls dynamically on button click.
Description:
By Using Place-Holder control I am going to generate Check box controls on the button click.
And How To Dynamically delete the controls.

  PlaceHolder.aspx

  • Steps1: Take PlaceHolder controls.
  • Step2:Take1 Text box to generate no of dynamic controls.
  • Step3:Take 1 button 
PlaceHolder.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class PlaceHolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < int.Parse(TextBox1.Text.ToString()); i++)
        {
         CheckBox chk = new CheckBox();
          PlaceHolder1.Controls.Add(chk);
          Button rdb = new Button();
            rdb.Text = "Delete";
            PlaceHolder1.Controls.Add(rdb);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));    
        }
        }
        }



Give the no you want to generate controls dynamically.

After Generate the controls if u want to delete click on the delete button to delete dynamically generated buttons.

Copyright © DotNet-Ashok