Posts

Antialiasing Text with C#

Image
Lately I have found myself having to use anti-aliasing in a lot of our rendering code to improve the output look our images.  To help others here is a snippet of code to anti alias text. protected override void OnPaint( PaintEventArgs e ) {     base.OnPaint( e );     Font font = new Font( "Tahoma", 12.0f, FontStyle.Regular );     e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;     TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 20 ), Color.Black );      e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;      TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 40 ), Color.Black );     e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;     TextRenderer.DrawText( e.Graphics, "Antialiased Text", font, new Point( 0, 60 ), Color.Black );     e....

C# Formatting DateTime to a String

Often times you find yourself needing to convert a DateTime object into a useful string representation.  Here are some basic parameters to the DateTime.ToString() method. MM/dd/yyyy 08/25/2010 dddd, dd MMMM yyyy Tuesday, 25 August 2010 dddd, dd MMMM yyyy HH:mm Tuesday, 25 August 2010 06:30 dddd, dd MMMM yyyy hh:mm tt Tuesday, 25 August 2010 06:30 AM dddd, dd MMMM yyyy H:mm Tuesday, 25 August 2010 6:30 dddd, dd MMMM yyyy h:mm tt Tuesday, 25 August 2010 6:30 AM dddd, dd MMMM yyyy HH:mm:ss Tuesday, 25 August 2010 06:30:07 MM/dd/yyyy HH:mm 08/25/2010 06:30 MM/dd/yyyy hh:mm tt 08/25/2010 06:30 AM MM/dd/yyyy H:mm 08/25/2010 6:30 MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy h:mm tt 08/25/2010 6:30 AM MM/dd/yyyy HH:mm:ss 08/25/2010 06:30:07 MMMM dd August 25 MMMM dd August 25 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2010-08-25T06:30:07.7199252-04:...

C# get number of messages in message queue (MSMQ)

Image
Working with message queues in .net is incredibly easy. However I often find myself having to get the number of message in queue for monitoring, etc. That aspect of the message queue framework is a bit lacking. There are a few options available but they all have some problems. You can call GetAllMessages() right off the message queue object. This code works, however it gets a copy of EVERY message queue item currently in the queue. Got 10 items, you get ten. Now do this with 10,000 items every second and it starts to become a problem. It's also possible to enumerate through all the messages in the queue and take a count of them manually but if your queue is busy this can cause invalid cursor exceptions to happen. This is the main reason I moved away from doing this. You can also do some performance counter work but I find that code to be too complex and very fragile and prone to breakage. I've found the best bet is to go with the COM/Win32 route. The nice thing here is ...

C# Serialize an object to/from XML

There have been many times I've needed to serialize an object to/from XML. .NET makes this incredibly easy. With a few meta tags your class(es) can easily be serialized and deserialized. You can then take this even further any start applying custom attributes to have as much granular control over the serialization as you want. For instance lets' take a very basic class with some data and properties. public class Person { int _age; String _name; /// Gets or sets the Age of the person. public int Age { get { return _age; } set { _age = value; } } /// Gets or sets the Name of the person. public String Name { get { return _name; } set { _name = value; } } /// Creates a new instance of the Person class public Person() { } } Now let's take this class and add the [Serializable] attribute. [Serializable] public class Person { int _age; String _name; /// Gets or sets the Age...

C# Equal vs CompareTo

Image
Out of curiosity I decided to run some speed tests on strings using the Equals and CompareTo methods. I wrote some quick code and came up with some interesting results: This test was run for 10000000 iterations. The Equals methods trounced the CompareTo method. Under the hood Equals stops when the first character that doesn't match is found. CompareTo continues on to the end of the string all the time. However this was a case insensitive search with no culture information. Adding more variables may cloud the water a bit, but that is going to be reserved for future posts. The code used in the test can be seen below. using System; using System.ComponentModel; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Threading; namespace Sandbox { class Program { static void Main( string[] args ) { const int counter = 10000000; int i = 0; int count = ...

C# using a background worker

This post is going to focus on creating and using a BackgroundWorker object. Background workers are found in the System.ComponentModel namespace. They're pretty easy to use. You just create a new object, assign a few properties and event handlers and let it do its thing. using System; using System.ComponentModel; First let's create a background worked object and set it up. Pstatic void Main( string[] args { // create the background worker object BackgroundWorker _worker = new BackgroundWorker(); // tell the background worker it can be cancelled and report progress _worker.WorkerReportsProgress = true; _worker.WorkerSupportsCancellation = true; // a worker thread object where the actual work happens WorkerThread thread = new WorkerThread(); // add our event handlers _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( thread.RunWorkerCompleted ); _worker.ProgressChanged += new ProgressChangedEventHandler( thread.ProgressCha...

C# Creating a Singleton

A lot of people talk trash about the Singleton and how it should be avoided like the plague. I think like a lot of things it has its place and can be used effectively in certain situations. I'm going to leave what those situations are alone for the time being and just focus on creating a nice thread safe lazy init singleton class. public sealed class Singleton { Singleton() { } public static Singleton Instance { get { return Nested.instance; } } class Nested { // Use an explicit static constructor to tell the C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly Singleton instance = new Singleton(); } }