Thursday, November 28, 2013

WPF : Making art with maths and SetPixel




Have you ever tried to create bitmap images with c# ?

Reading or writing pixels is very very easy...
Each pixel has RGBA value that can you can read or write.

R : the Red component of the pixel
G : the Green component of the pixel
B : the Blue component of the pixel
A : the Alpha (transparency level) component of the pixel

Each component can have a value between 0 and 255 in decimal (base 10) and between 00 and FF in hexadecimal (base 16).


This sample code use simple math function to calculate random colors for pixels.


You can move the slider to apply a multiply factor to animate the generated bitmap.


This is a really simple sample but you can quite easily apply filters or draw fractals for exemple !


You can download the source code of this sample project (VS2012 / C#)
WpfArtWithMath.zip











Read More

Monday, November 11, 2013

WPF : Easy Text To Speech (speech synthesis from text)



There is two way to generate speech from text with WPF :

- Using the Windows System.Speech library that comes with .net framework 3.5
- Using a distant web service such as Google Text2Speech API


In this article, we will focus on the second way to do that, that means we will call Google REST web services to generate a MP3 file from a sample text.
We will use nAudio (http://naudio.codeplex.com) to play the resulting MP3 file into our application.

Ok, let's start !

The Google API URL to call is : 
http://translate.google.com/translate_tts?tl=[CULTURE]&q=[YOUR_TEXT]

For example : 
http://translate.google.com/translate_tts?tl=en&q=Hello+world


Culture can be : en, fr

Here is a list of the supported languages :
English, Afrikaans, Albanian, Catalan, Chinese (Mandarin), Croatian, Czech, Danish, Dutch, Finnish, Greek, Hungarian, Icelandic, Indonesian, Latvian, Macedonian, Norwegian, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Swahili, Swedish, Turkish, Vietnamese, Welsh, Haitian Creole, French, Italian and German...

Your text must be string with %20 or + instead of spaces (ex : &q=Hello%20world or &q=Hello+world)

Keep in mind that an URL shouldn't be more than 255 caracters otherwise it can be truncated by routers, proxies...

More info about that API :
http://techcrunch.com/2009/12/14/the-unofficial-google-text-to-speech-api

The c# code is quite simple, it just make an HTTP call to the Google URL and save or play the resulting audio stream.


Have fun with that :)

You can download the source code of this sample project (VS2012 / C#) :
WPFText2Speech.zip



Read More