Archive for February, 2008

Xml Serialization

If you want to store data, you often use a database. It’s very good way. But, sometimes you need to save too little amount of data that using a database is thoughtless. Another way to store data is to put it in a simple text file. One of the most convenient ways to do this is to use a XML file. It stores data in such a way, that you can easily access it later. XML files are used everywhere and for every type of usage. For example, two (or more) computers can communicate with XML files.

I am going to show you how you can convert your object to a XML file in order to save its data. This process if called serialization. There are many ways to serialize an object. I am going to show you how you can you do this by using the IXmlSerializable interface. It’s in System.Xml.Serialization namespace. You’ll need to include System.Xml namespace, too. (more…)

Filled under C#. No Comments.

Simple Gradient

A simple was how to draw a gradient on a form. All you need is to include both Drawing and Drawing2D namespaces in your project. As we are going to draw onto the form, we need to assure, that if user resizes it, we will have our gradient spread over the whole form. We can do this by using form’s Resize event. Each time the form is being resized, we tell it to repaint.

private void Form1_Resize(object sender, EventArgs e)
{
	this.Invalidate();
}

In order to paint on the form, we are going to use its Paint event. In this way, the next time a message to repaint is received, we will have our gradient repainted.

The class LinearGradientBrush creates a special brush, which we can use in our further work. It accepts four arguments:

  1. a rectangle, which determines the area where we are going to paint
  2. color one
  3. color two
  4. an angle

Here is a simple example.

private void Form1_Paint(object sender, PaintEventArgs e)
{
	e.Graphics.Clip = new Region(ClientRectangle);
	LinearGradientBrush gradient = new LinearGradientBrush(ClientRectangle, Color.Peru, Color.Pink, 90f);
	e.Graphics.SmoothingMode = SmoothingMode.HighQuality; // set the quality
	e.Graphics.FillRectangle(gradient, ClientRectangle);
 
	gradient.Dispose();
}

Gradient

Filled under C#. No Comments.

Filesize To String

I’ve made a simple function that converts a file size to a string. It computes the remainder after dividing the input size by 1024 and puts it in an array. This step is being performed while we have a size under 1024 – this is the number of the bytes. (more…)

Filled under C#. No Comments.

Allow Only One Instance

Sometimes you need your application to have no more than one instance. Here comes the Mutex Class.
When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex. (more…)

Filled under C#. No Comments.

Matrices

I have constructed a class library to perform different operations with matrices. I was inspired by my university course of Linear Algebra. This library is still beta, so don’t wonder if you encounter a problem.

In mathematics, a matrix is a rectangular table of numbers. Instead of using integers only, I have used my fraction class to represent numbers in the matrix. You can sum matrices, multiply them, determine their determinant and their rang, inverse & transpose them. (more…)

Filled under C#. No Comments.