Monthly Archives: September 2014

Convert between byte arrays and hexadecimal strings in C#

This example uses extension methods to convert between byte arrays and hexadecimal strings. It adds a ToHex extension method to the byte[] type and a ToBytes extension method to the string class. To create an extension method, create a public … Continue reading

Posted in algorithms, cryptography, extensions, strings | Tagged , , , , , , , , , , , , , , , | 2 Comments

Generate random strings in C#

Sometimes it’s useful to generate random strings, often to test a program such as one that sorts strings. The Random class’s Next method generates random numbers. To make random words, you can make an array of letters and then use … Continue reading

Posted in algorithms, strings, variables | Tagged , , , , , , , , , | Leave a comment

Draw a fractal generated by prime numbers in C#

This example shows how to draw a fractal generated by prime numbers. (For information and discussion of this idea, see The prime-explosion Bergot graph at The Prime Puzzles & Problem Connection.) The program draws the image by following these rules: … Continue reading

Posted in algorithms, fractals, graphics, mathematics | 1 Comment

Map numeric values to and from colors in a color gradient in C#

Sometimes it’s useful to map numeric values to and from colors. For example, intensity of color could indicate population density, agricultural yield, rainfall, or other values on a map. This program draws a color-selection rainbow that includes the colors red, … Continue reading

Posted in algorithms, graphics, mathematics | Tagged , , , , , , , , , , , , , | 8 Comments

Map numeric values to colors in a rainbow in C#

Sometimes it’s useful to map numeric values to colors. For example, intensity of color could indicate population density, agricultural yield, rainfall, or other values on a map. The following MapRainbowColor method maps a value between two bounds to a rainbow … Continue reading

Posted in graphics, mathematics | Tagged , , , , , , , , , , | 3 Comments

Let the user drag an image with transparent pixels over a background image in C#

This example shows how you can let the user drag an image on a PictureBox. The PictureBox‘s Image property is set to a background image. The user can press the mouse down on any of the image’s non-transparent pixels to … Continue reading

Posted in graphics, image processing | Tagged , | 2 Comments

Zoom on a graph with XAML code in C#

This example shows how you can zoom on a graph with XAML code only, without using C# code. (The program does use C# code to build the graph. It just doesn’t use any to zoom.) The example Zoom in on … Continue reading

Posted in algorithms, drawing, geometry, graphics, mathematics | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Zoom on a graph in WPF and C#

It’s remarkably easy to zoom on a graph in WPF. This example extends the example Let the user click on graph points in WPF and C#. To modify the previous version, you first need to rebuild the user interface a … Continue reading

Posted in algorithms, drawing, geometry, graphics, mathematics | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | 2 Comments

Let the user click on graph points in WPF and C#

This example shows how you can let a user click on graph points to display information about the data. (It extends the example Draw a graph with rotated text in WPF and C#.) When the user moves the mouse over … Continue reading

Posted in algorithms, drawing, geometry, graphics, mathematics | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | 1 Comment

Draw a graph with rotated text in WPF and C#

This example shows how you can draw a graph with rotated text. The example Draw a graph with labels in WPF and C# has a DrawText method that makes adding text to a graph relatively easy. This example uses the … Continue reading

Posted in algorithms, drawing, geometry, graphics, mathematics | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | 2 Comments