C# Snippet for Accessing Bitmap Data with Unsafe Code

Since GetPixel is slow and I keep having to access bitmap data with unsafe code, I thought it would be a nice thing to make into a snippet. Continued reading >

Image.Clone does not unlock the file

Turns out, copying an image using Clone really does make an exact copy. You can’t just open an image, clone it, then safely close it as you might expect, because it also copies the file lock.

Instead, copy the image in memory using Image copy = new Image(source).

Automatically Fill a ComboBox with an Enum

This is a really nice little feature.

If we have an enumerator, say:

public enum MyEnumerator { Oranges, Apples, Hedgehogs, Laptops, Budgies, Other }

Having a form with a ComboBox called comboBox1, put this in the constructor/form load etc:
this.comboBox1.DataSource = Enum.GetValues(typeof(MyEnumerator));

This fills the combobox with the values. To convert the current combobox value back into the enumerator’s value:
MyEnumerator selected = (MyEnumerator)this.comboBox1.SelectedValue;

Things I can’t code without in Visual Studio

Here’s a quick list of essential plug-ins and tools for Visual Studio. All of them are free, and this is mainly so the next time I reinstall windows I won’t spend ages trying to remember what that thing I use was called.

Microsoft StyleCop: http://code.msdn.microsoft.com/sourceanalysis
This is the source analysis tool from and used by Microsoft programmers, and helps to “enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code”. It can also be integrated it into the build process to help keep you honest.

Once you learn and get used to its strict rules, they are easy to live by (I’ve even grown used to using this.variable rather then _Variable ).

GhostDoc: http://www.roland-weigelt.de/ghostdoc/
With just a keypress this adds in XML comments for the current type, often magically filling in the values from base types, other comments, or who knows where. Saves a lot of time, and it’s perfect for all those StyleCop “must have a documentation header” warnings.

Regionerate http://www.rauchy.net/regionerate/
Great little plugin that automatically arranges your code based on layout rules. Produce neat and friendly code without any effort. Check out this post in their forum for a StyleCop layout: http://www.rauchy.net/regionerate/forums/viewtopic.php?f=4&t=189. The only issue is that it uses tabs instead of spaces, but you just have to remember to select all (Ctrl-A), then format it (Ctrl-K-F).

Microsoft FxCop: http://code.msdn.microsoft.com/codeanalysis
Microsoft code analysis tool. It looks at the compiled code and produces a list of possible problems based on Microsoft’s design guidelines.

Proggy Fonts: http://www.proggyfonts.com/index.php?menu=download
A collection of small, neat, and easy to read fonts. They’re all good, but I tend to stick with Proggy Square.

The DirectSound Capture Buffer

A CaptureBuffer is a buffer of a specified length that reads audio from a Capture object which represents the capture (or input) device, into a one-dimensional array of bytes with a specified WaveFormat.

Continued reading >

An Explanation of Output Buffers in DirectSound

A DirectSound Buffer is a one-dimensional array of bytes which represent sound waves in a digital form. The data is laid out in a specified Microsoft.DirectX.DirectSound.WaveFormat which specifies the format type, number of channels, number of samples per second and so on.1

Each byte is a part of a sample, and a sample represents the average amplitude of the sound waves position at that point in time.2

There are two types of buffers used for output: Primary and Secondary. These both send data to an output device, represented by a Microsoft.DirectX.DirectSound.Device object.
Continued reading >

  1. Only data which matches that format can be loaded into the an output buffer. Sound data with different formats can be played at the same time by using separate secondary buffers for each. []
  2. Remember that sound is a waveform, and the buffer contains the actual wave data. Filling it with just one value will produce silence as the speaker cones will not move. To produce a tone programatically you need to fill the buffer with a waveform, such as a Sine Wave, a Square Wave, a Triangle Wave, or a Sawtooth Wave. []

Getting the next n birthdays from a table in SQL

If we have a table with a basic structure like this:
---------- person ---------- person_id name dob ... ----------

Let’s run a query with the results sorted by order of upcoming birthdays:

SELECT *,
IF(MONTH(`dob`) < MONTH(NOW()) ||
	(MONTH(`dob`) = MONTH(NOW()) && DAY(`dob`) < DAY(NOW())),
	MONTH(`dob`) + 12, MONTH(`dob`)) AS `adjusted_month`
FROM `person`
ORDER BY `adjusted_month`, DAY(`dob`), `name`

An alias called adjusted_month is used.

If the month of birth is earlier then the current month, or if it is the same and the day of birth is earlier then the current day, adjusted_month is set to the the month of birth + 12. Otherwise adjusted_month is just set to the month of birth.

The results can then be sorted by adjusted_month and the day to get the desired result.

Number of Selected Items in a Multiple Select Box with jQuery

With the html:

<select name="my_list" multiple="multiple">
    <option value="one">1</option>
    <option value="two">2</option>
    <option value="three">3</option>
    <option value="four">4</option>
    <option value="five">5</option>
</select>

One way to find the number of options selected:
$("select[name='my_list']>option:selected").length

TextRenderer is slow, DrawString is wrong

After struggling with WinAPI code and hacks in .NET 1.0 I was really looking forward to the TextRenderer class when I first heard about it. But as it turns out, not only is TextRenderer horrible at measuring character size it is also monumentally slow at rendering text. Continued reading >

Dragging and dropping onto a RichTextBox

One of many flaws with the RichTextBox control is its lack of support for drag/drop operations. Luckily it’s simple to add. In fact, it’s so simple you’ll wonder why they didn’t just support it out of the box. Continued reading >