Author Archives: Rod Stephens

Remove non-printable ASCII characters from a string in C#

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string. public static string TrimNonAscii(this string value) { string pattern = “[^ -~]*”; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, “”); } In ASCII, the printable characters lie … Continue reading

Posted in extension methods, strings | Tagged , , , , , , , , , , , , | 5 Comments

Use “banker’s rounding” and “normal rounding” in C#

By default the Math.Round method uses “banker’s rounding.” In banker’s rounding a number with a final digit of 5 is rounded to the nearest even number rather than to the next larger number as you might expect. The idea is … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , , , | Leave a comment

See if a file is locked in C#

Some applications lock files so you cannot write, read, delete, or otherwise mess with them. For example, when you open a file in Microsoft Word, it locks the file so you cannot delete it or open it for writing with … Continue reading

Posted in files, system | Tagged , , , , , , , , , , , , , , | 3 Comments

Map between host names and IP addresses in C#

When you enter a host name and click Go, the program uses the following code to look up the host and display the IP addresses associated with it. using System.Net; … // Display the entered host’s IP address. private void … Continue reading

Posted in internet, network, system | Tagged , , , , , , , , , , , , , , , | Leave a comment

List available culture codes in C#

This example shows how you can make a list of the culture codes that are available on the system. Several C# Helper examples deal with globalization including Localize a program in C#. To localize a program, you need to specify … Continue reading

Posted in globalization, internationalization, system | Tagged , , , , , , , , , , , , , | Leave a comment

Make a cannon game in C#

This simple cannon game randomly positions a target house. Then when you click the Shoot button, it gets angle and speed information for the cannon and enables a timer. To keep the graphics and timing simple, the program uses a … Continue reading

Posted in algorithms, animation, games, graphics | Tagged , , , , , , , , , , , , , | Leave a comment

Convert a bitmap into a cursor in C#

Converting a bitmap into a cursor isn’t easy, but you can do it by using the CreateIconIndirect API function. This example uses the following BitmapToCursor method to create a cursor from a bitmap. // Create a cursor from a bitmap. … Continue reading

Posted in graphics, image processing | Tagged , , , , , , , , , , , , , | Leave a comment

Use a bitmap for an icon in C#

You can’t set a form’s Icon property equal to a bitmap. Fortunately it’s easy enough to create an icon from a bitmap. This example uses the following code to make a form’s icon display the image in a bitmap. // … Continue reading

Posted in forms, graphics, image processing | Tagged , , , , , , , , , , | Leave a comment

Make a tool that creates PNG files with transparent backgrounds in C#

When you use the File menu’s Open command, the following code lets you select a image file. // The image. private Bitmap Bm = null; // Offset for displaying the image. private const int Offset = 10; // Open a … Continue reading

Posted in graphics, image processing | Tagged , , , , , , , , , , , , , | Leave a comment

Give an image a transparent background in C#

The Bitmap class’s MakeTransparent method changes all of the pixels with a given color to the transparent color A = 0, R = 0, G = 0, B = 0. When the program starts, the following code makes the background … Continue reading

Posted in graphics, image processing | Tagged , , , , , , , , , , , , | Leave a comment