c# (29)


Split string without breaking words

Let say we got a long string that need to be splitted in a series of rows (of a certain lenght) . If the length exceed, we need to go wrap the text on a new line ensuring words are not broken. The solution is quite simple after all, before going to the new line […]




Create an image from text (Updated)

I’ve done a few changes and some additions on the code of the previous post. The CreateTextImage now prints the text at the center of the image, this is done using the Alignment property of the StringFormat class: public Bitmap CreateTextImage(string imageText) { Bitmap outputImage = new Bitmap(1, 1); int imageWidth, imageHeight = 0; //setup […]




C# 4.0 Parallel features performance comparison

In order to exploit the power of multicore processor, the fourth version of the framework ships the Parallel features, easiest alternatives for multithreaded operations execution. With my (old) ASUS PRO60E, a Core 2 Duo T8100 2.1 GHz with 4GB of ram and Win7 x64, I made some tests to find out the performance differences between […]




Publish Rss feed using the Syndication namespace

Here’s an easy way to expose feeds using the new System.ServiceModel.Syndication namespace. Instead of dealing with XmlDocument the Rss20FeedFormatter class will be responsible to format the correct xml content for our feed. 1: SyndicationFeed feed = GetSyndicationFeed(); 2: Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed); 3: using (XmlWriter writer = XmlWriter.Create(context.Response.OutputStream)) 4: { 5: rssFormatter.WriteTo(writer); 6: writer.Flush(); […]




Ordinal suffixes with a custom IFormatProvider

Ordinal suffixes for english dates are composed by a number and 2 characters. Some classes of the framework provide an usueful implementation of the IFormatter provider, like the Datetime class which allow the developer to display the date in various ways, even with ordinal suffixes. If we  need to display the value of an object […]




ImageResizer

Finite le vacanze, tutti a metter le proprie foto online. Ecco una piccola utility (frutto di refactoring e copia/incolla)  per ridimensionare ed alleggerire le immagini in un formato adatto al web: Program.cs 1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.IO; 6: 7: namespace ImageResizer 8: { 9: class […]




Workflow/Webservice async operations

Tra le tante novita’ delle ultime versioni del framework una delle mie favorite e’ sicuramente Workflow Foundation. Il poter modellare il flusso della propria applicazione, spostarne le parti (se ben disegnato) senza rimettere mano al codice, raggruppare activities in blocchi come la SequenceActivity e gestirne tutte le eccezioni con un FaultHandler all’altezza dell’activity che le […]




XElement e liste

Ottenere un documento XML dove un elemento/nodo puo’ essere ripetuto piu’ di una volta (ad esempio il risultato di una query sql) utizzando liste generiche: 1: public class Persona 2: { 3: public Persona(string nome, string cognome) { this.nome = nome; this.cognome = cognome; } 4: public string nome, cognome; 5: } 1: List<Persona> listPersone […]




Iphone video converter

Come possessore di Iphone mi capita di dover convertire e trasferire filmati sul bel gioellino di casa Apple: essendo la maggioranza dei convertitori video a pagamento ed i pochi free(vedi Videora) farciti di banner ecco un tool poco invasivo in grado di convertire la maggior parte dei formati video in files video per Iphone/Ipod. L’applicazione […]