Saturday, November 28, 2009

Write .ICO files [C#]

Greetings. Here's a console application I wrote, which uses the BinaryWriter to create icon files (.ico image file format). It is based on these specifications.
[Note: .NET ImageFormat.Icon doesn't really write .ico files]

You can drag and drop one or more .NET compatible image files over the app icon (equivalent to console arguments).
The program creates a Bitmap of each image, and checks if it is within the 256x256 pixels limit. Then, the Bitmap data is used by the BinaryWriter to create the converted file.

CreateNew mode on the FileStream avoids file overwriting.
Since the app is targeted to run from its' icon, exceptions are shown via MessageBoxes.

Saturday, November 7, 2009

Snake Game [C#]

Hello again. I wrote a snake game in C#, let me tell you a little bit about it:

The game features two classes of sprites: Vertebra, representing a single snake link; and Pizza, which is, of course, the pizza.
Each of these classes inherits from the PictureBox class, and uses a Timer for a nice animation when these objects appear.

Game pause is achieved simply by start and stop of the game Timer.

There is a top 10 high scores, which is stored in an xml file.
This file is created automatically on the first time it is requested by the game.
The handling of the high scores is done with LINQ to XML, which is awesome!

Here is the code for the Main form:

Here is the code of the Vertebra Class:

And here are a couple of screenshots:

Monday, October 26, 2009

Paint Application [C#]

Greetings. I wrote a Paint Application in C#, here is a summary of it:

It is a Multiple-Document Interface application - The Image forms are MdiChildren of the Main application form.
According to this, the main menu is merged with a non-visible MenuStrip on the Image form.
The various tool windows are owned by the Main application form.

The featured tools are: Select, Pencil (1px free-draw), Brush, Eraser, Rectangle, Circle, Fill and Pipette (color picker).

The different parameters for each tool are displayed in the Tool Options window.
Each tool's options are arranged on a dedicated panel on this form. When a tool is chosen, its' options panel toggles to the only visible panel.

The edited image is displayed inside a PictureBox control.
All drawing actions are done in the course of the MouseDown, MouseMove and MouseUp events of that PictureBox.
A boolean named isDragging is turned true on MouseDown event, and false on MouseUp event, thus enabling continuous drawing actions.

Selection definition, dragging and dropping is done in a similar way, based on four selection states listed in an enum.

The algorithm for the flood fill tool is taken from this tutorial.

Two image filters are available from the Image menu - Brightness and Invert, and can also be applied to selections.
Brightness also features a dialog for setting the filter level, with a small preview of its' effect.

The data for the multiple undo and redo features is stored in Stacks.

Image files can be loaded and saved in the various ImageFormats provided by .NET.

Here are a couple of screenshots:

And here is the code for the Image form:

Monday, October 5, 2009

VBScript Editor [C#]

Hi there. I wrote a simple VBScript Editor in C#, here is a summary of it:

It is a Multiple-Document Interface application, the main menu is merged with a non-visible MenuStrip on the Document form.

Syntax coloring and letter casing is done live, based on the lines affected by the change in the text. The keywords for each color are stored in a regular expression.

Line numbers and column ruler are achieved by RichTextBoxes moved on separate panels. Line and column markers are updated according to the caret's position.

The data for the multiple undo and redo features is stored in Stacks.

Printing is available using the RichTextBoxPrintCtrl from Microsoft.

The tool windows, 'Find\Replace' and 'Jump to line' are always owned by the current active Document.

Here is the code for the Document form: