C#朗读文章

来源:互联网 发布:damn small linux 编辑:程序博客网 时间:2024/04/29 18:04

先在C盘下找到sapi.dll文件,添加的引用

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

namespace WindowsApplication1
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
try
            
{
                SpeechVoiceSpeakFlags spFlags 
= SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SpVoice voice 
= new SpVoice();
                
if (this.richTextBox1.Text.Trim() == string.Empty)
                
{
                    voice.Speak(
"请输入数据!", spFlags);
                }

                
else
                
{
                    voice.Speak(
this.richTextBox1.Text, spFlags);
                }

            }

            
catch (Exception Err)
            
{
                MessageBox.Show(Err.Message);
            }

        }

    }

}

原创粉丝点击