Monthly Archives: October 2016

Draw numbered circles and save them into files in C#

Sometimes I need to draw numbered circles to place on pictures that I’m going to use in books or articles. This program lets me draw circles that look nice, that are smoothly shaded, and that have transparent backgrounds. The MakeNumberBitmap … Continue reading

Posted in drawing, fonts, graphics | Tagged , , , , , , , , , , | 3 Comments

Animate images in C#

This example shows how to animate images by loading them at run time and then playing them one at a time. To make finding the images easier, I added them to the project. (Open the Project menu, select Add Existing … Continue reading

Posted in algorithms, animation, image processing | Tagged , , , , , , , , , , | Leave a comment

Draw gears in C#

This example uses the following DrawGear method to draw gears. // Draw a gear. private void DrawGear(Graphics gr, Brush axle_brush, Brush gear_brush, Pen gear_pen, PointF center, float radius, float tooth_length, int num_teeth, float axle_radius, bool start_with_tooth) { float dtheta = … Continue reading

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

Simulate mouse movement and clicks in C#

This program uses the mouse_event API function to simulate mouse movement and to simulate a mouse click. The program’s Paint event handler draws some circles around a target point so you can see where it is. // The mouse’s target … Continue reading

Posted in API, miscellany, user interface | Tagged , , , , , , , , , , , , | 1 Comment

Let a thread call a form’s methods in C#

When the user clicks the Start Thread button, this program runs a counter on a separate thread. Every second the counter updates the form’s Value variable. It then displays the thread’s number and the new Value in the ListBox at … Continue reading

Posted in algorithms, programs, system, threading | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Run threads with different priorities in C#

This example runs several different threads at different priorities. Each of the threads executes the following Counter class’s Run method. class Counter { // This counter’s number. public string Name; // Initializing constructor. public Counter(string name) { Name = name; … Continue reading

Posted in algorithms, programs, system, threading | Tagged , , , , , , , , , , , , , , , , , , , , | Leave a comment

Sort words by letter count in C#

This example sorts words by letter count. It counts the number of distinct letters in a list of words and displays the words and their counts sorted by the counts. The program is remarkably simple. The following code shows how … Continue reading

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

Count pixels of different colors in C#

The following CountPixels method counts pixels in an image that match a target color. // Return the number of matching pixels. private int CountPixels(Bitmap bm, Color target_color) { // Loop through the pixels. int matches = 0; for (int y … Continue reading

Posted in algorithms, graphics, image processing | Tagged , , , , , , , , , , | 3 Comments

Define and use bit masks in C#

The example Understand bit masks in C# explains how to use bit masks. To define a bit mask, simply create an enum and give it the Flags attribute as in the following code. [Flags] private enum BitmaskEnum { Value1 = … Continue reading

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

Understand bit masks in C#

Some values, including some properties defined by the .NET Framework, are bit masks. That means each bit in a value means something. For example, the AnchorStyles enumeration that determines how controls are anchored in their parents defines four values: Top, … Continue reading

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