Sunday, 24 May 2009

Using PATINDEX for Regular Expressions in T-SQL

I recently needed to perform some validation using regular expressions in T-SQL rather than C#. Fortunately, this is easily accomplished using the PATINDEX keyword. For example:

PATINDEX('[^0-9]%',@Input)

The above example would check that the variable @Input does not begin with a numeric character.

Labels: ,

Bookmark and Share

Friday, 3 April 2009

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled

This error is caused when an aspx page contains a GridView which has pagination enabled, but the PageIndexChanging event has not been handled.

This had me stumped for a short while today whilst coding a C# ASP.NET web application. If you have pagination turned on, you'll need to create a handler for your GridView, thus:


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

// Increment Page Index
GridView1.PageIndex = e.NewPageIndex;

// Need to rebind or page does not change
GridView1.DataSource = MyDataSet;
GridView1.DataBind();

}

Note that in the above example I also explicitly set the new PageIndex, and rebind the DataSource. The reason being that if you don't, changing pages doesn't work.

Labels: ,

Bookmark and Share

Saturday, 28 March 2009

Speeding up GetPixel with Unsafe Code, Part II

In relation to my previous article on replacing GetPixel, I didn't include an example of how to replace SetPixel. So if you haven't figured it out, here's an example that will colour every pixel in a bitmap red, which should point you in the right direction.

private static void SetPixelExample(ref Bitmap bitmap)
{
int CurScanLine = 0;
Rectangle Rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData TempBitmapData =
bitmap.LockBits(Rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

while (CurScanLine < bitmap.Height)
{
for (int x = 0; x < bitmap.Width; x++)
{
SetPixelNew(ref TempBitmapData, x, CurScanLine, Color.Red);
}
CurScanLine++;
}

bitmap.UnlockBits(TempBitmapData);
}

private static unsafe void SetPixelNew(ref BitmapData bitmapData, int x, int y, Color colour)
{
byte* PixelPointer = null; // Pointer to our pixel data
int ColourPlanes = 3;
PixelPointer = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
PixelPointer[ColourPlanes * x] = colour.B;
PixelPointer[(ColourPlanes * x) + 1] = colour.G;
PixelPointer[(ColourPlanes * x) + 2] = colour.R;
}

I've used a similar technique to write a speedy PCX image file decoder which decodes the PCX file, places it in a byte array then writes the colour data to a bitmap, which is one possible application to consider.

Labels:

Bookmark and Share

Tuesday, 24 March 2009

Speeding up GetPixel with Unsafe Code

Although GDI+ provides a method, GetPixel, for getting the colour of a particular pixel at a particular set of co-ordinates, you'll quickly discover how slow it is when you come to examine every pixel in even an average sized bitmap.

This is due to
GetPixel locking, then unlocking the bitmap into system memory every time GetPixel is called. The process of locking and unlocking the bitmap itself is what takes the time.

However, with the aid of a little unsafe code, you can easily speed this up. For those unfamiliar with the unsafe keyword, in C#, putting the keyword unsafe before a method generally means it uses pointers. By default, the Visual Studio IDE will not let you compile unsafe code. You'll need to either use the /unsafe compiler switch if compiling from the command line, or tick the "Allow unsafe code" checkbox in the project properties if you're using the Visual Studio IDE:




So why the need for unsafe code?

Instead of using
GetPixel to get the colour of each pixel, we'll lock the bitmap into system memory, which will then give us direct access to the bitmap data via the BitmapData class and the use of a byte pointer. Only when we're finished with the data will we unlock the data, thus saving us potentially thousands of locking and unlocking operations.

To illustrate the speed difference I've written a small program which makes use of two functions. One returns a list of Colors obtained by using the existing .NET
GetPixel method. The second returns a list of Colors via unsafe code.

The speed of the two functions is also measured and displayed crudely.


private void TestGetPixelMethods()
{
Bitmap B = (Bitmap)Bitmap.FromFile(@"C:\MyImage.jpg");

// Execute and Time Managed Code
DateTime StartTime = DateTime.Now;
GetAllPixelsSafe(B);
DateTime FinishTime = DateTime.Now;
TimeSpan Duration = FinishTime - StartTime;
Debug.WriteLine(string.Format("GetAllPixelsSafe : {0} ", Duration));

// Execute and Time unsafe code
StartTime = DateTime.Now;
GetAllPixelsUnsafe(B);
FinishTime = DateTime.Now;
Duration = FinishTime - StartTime;
Debug.WriteLine(string.Format("GetAllPixelsUnsafe: {0} ", Duration));
Debug.WriteLine(string.Empty);
}

/// <summary>
/// Returns a list of Colors representing each pixel in the passed image
/// </summary>
private List<Color> GetAllPixelsSafe(Bitmap b)
{
List<Color> ColorList = new List<Color>();

for (int x = 0; x < b.Width; x++)
{
for (int y = 0; y < b.Height; y++)
{
ColorList.Add(b.GetPixel(x, y));
}
}

return ColorList;
}

/// <summary>
/// Returns a list of Colors representing each pixel in the passed image
/// Uses unsafe code
/// </summary>
private unsafe List<Color> GetAllPixelsUnsafe(Bitmap b)
{
Rectangle Rect = new Rectangle(0, 0, b.Width, b.Height);
BitmapData TempBitmapData =
b.LockBits(Rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

byte* PixelPointer = null; // Pointer to our pixel data
int ColorPlanes = 3; // 3 Color Planes in a 24bpp image
List<Color> ColorList = new List<Color>();

for (int x = 0; x < b.Width; x++)
{
for (int y = 0; y < b.Height; y++)
{
PixelPointer = (byte*)TempBitmapData.Scan0 + y * TempBitmapData.Stride + ColorPlanes * x;
ColorList.Add(Color.FromArgb(PixelPointer[2], PixelPointer[1], PixelPointer[0]));
}
}

b.UnlockBits(TempBitmapData);

return ColorList;
}

When
TestGetPixelMethods is called, the two pixel reading methods are called in turn, and the length of time elapsed for each is output to the debug console.

As this is just an example, note that
GetAllPixelsUnsafe assumes that a 24bpp image is passed. If a 32bpp image were to be passed, PixelFormat.Format32bppArgb would be used and the integer ColorPlanes would need to be set to 4, as a 32bpp image also contains an alpha channel.

Here is the output to my debug console:



Although the measurements are crude, notice that in most cases the method that uses the .NET GetPixel function is taking 0.266s. The method that is using unsafe code is taking 0.0625s. That's about 4.25 times faster, and well worth taking the trouble to implement with image manipulation code that works at the per-pixel level.

Of course, a similar function could be written to replace
SetPixel, which, armed with the above knowledge, you should now be able to implement yourself!

kick it on DotNetKicks.com

Labels:

Bookmark and Share

Saturday, 21 March 2009

Repeater Control Words of Wisdom

Non-mental note to self.

When using the .NET Repeater Control in which there is a child CheckBox or RadioButton present to allow for data items to be selected, ensure that
autopostback = true for the child control you are using.

If
autopostback is not true, the value of the Checked property will always be false when the RadioButton or Checkbox has been selected and you come to use FindControls in order to determine the state of your child control:
protected void Button1_Click(object sender, EventArgs e)
{
bool CheckedValue;

foreach (RepeaterItem Item in Repeater1.Items)
{
CheckBox CB = (CheckBox)Item.FindControl("MyCheckBox");
CheckedValue = CB.Checked;
}
}
Also ensure that string passed into the "FindControl" function matches the name of the control you are looking for. Not that I'd be foolish enough to make such an elementary typo, of course.

Labels: ,

Bookmark and Share

Monday, 16 March 2009

Using ConfigurationManager to Read the Configuration File in C#

I've seen this done in so many convoluted ways, yet it's very, very simple and only requires one line of actual code. There's a rather lengthy example on MSDN that doesn't help to clarify how easy it is to read a value out of the configuration file and 95% of the time, that's all you want to do.

Firstly, add app.config to your project. (Obviously, if you were developing a web application, you'd be using web.config)

Create an <appsettings> section, and then add the key value pairs that you want to store within it, for example:


<configuration>
<appsettings>
<add key="MagicNumber" value="3">
</add>
</appsettings>
</configuration>

Add a reference to System.Configuration into your project, and add "using System.Configuration" at the top of your code. You can then write the code:


void GetAppSettings()
{
int MagicNumber =
Convert.ToInt32(ConfigurationManager.AppSettings["MagicNumber"]);
}

You'd probably want to add exception handling for a missing or malformed settings file, but that is quite literally, it.

Still people ask, but as they have a habit of saying on Who Wants to be a Millionaire - it's only easy if you know the answer...

Labels: ,

Bookmark and Share

Tuesday, 10 March 2009

Obfuscating a String with XOR in C#

Related to my recent posts on conversion and bitwise operators, I'm going to talk a little bit about obfuscating a string with XOR as a practical example of both string to byte conversion, and the bitwise XOR operator.

Although this could also be considered encryption in some schools of thought, on its own this is so weak as an encryption technique I prefer to think of it as obfuscation.

XOR obfuscation does have its place. It can be used to easily thwart simple plain text searches, for example. If you have hardcoded strings in an assembly, you could hardcode obfuscated strings instead, so that the plain text string value does not show up in a search. You could also apply obfuscation before subsequently performing a secure encryption method, thus making it harder for a determined hacker to determine whether or not a valid decryption has been performed - since the decrypted string will still be obfuscated.

But by itself, think of it as one of those magic pens that writes in ink that is only visible under ultraviolet light. Enough to protect data from a casual user or passer-by, but absolutely useless in protecting information from someone that knows what they're trying to do and has some degree of competency.

Here's a function that will obfuscate a string then:


private string XORString(string inputString, byte key)
{
ASCIIEncoding Encoder = new ASCIIEncoding();
byte[] ByteArray = Encoder.GetBytes(inputString);
for (int i = 0; i < ByteArray.Length; i++)
{
ByteArray[i] ^= key;
}

return Encoder.GetString(ByteArray);
}

Pretty simple. Convert our input string into a byte array, then XOR each byte in the array with a key value, and return the reconstructed string.

And here's why you shouldn't rely on this technique to protect anything you want to keep secret:


private void BruteForceXORString(string inputString)
{
ASCIIEncoding Encoder = new ASCIIEncoding();

byte XORVal = 0;
byte[] ByteArray;

while (XORVal < 255)
{
ByteArray = Encoder.GetBytes(inputString);

for (int i = 0; i < ByteArray.Length; i++)
{
ByteArray[i] ^= XORVal;
}

Debug.WriteLine(string.Format(
"{0}: {1}",
XORVal,
Encoder.GetString(ByteArray)));

XORVal++;
}
}

In next to no time, this function will output every possible decoding of an obfuscated string which has been XOR'd with a byte value. Say I'd obfuscated the string "Visual Studio" with an XOR value of 16:


As you can see, there is the original string plain to see after very little effort.

Use with caution, and always ask yourself, "Am I happy hiding this information with a virtual magic pen?"

Labels: ,

Bookmark and Share

Thursday, 5 March 2009

C# Bitwise Operators

It's essential to master the logical bitwise operators if you're working with the individual bits of a byte on a regular basis. In the real world, it's especially useful when interfacing with industrial hardware (hardware engineers love digital I/O), working with flags, or if you're writing an emulator, in which case it's what you'll spend a lot of time doing. So here's a quick reference.

Note that you wouldn't usually create a seperate function just to perform a logical bitwise operation, I've done it in the examples below for clarity.

To test the value of a bit, use the Logical AND operator. In the following code, the bit parameter is the bit(s) to check, and the mask parameter is the value in which to test the set bit.

private bool TestBit(ushort bit, uint mask)
{
// AND
return (mask & bit) == bit ? true : false;
}

i.e. to check if bit 4 was set in a uint called MyValue:

bool Result = TestBit(8, MyValue);

To set the value of a bit, use the Logical OR operator.

private void SetBit(ushort bit, ref uint mask)
{
// OR
mask = (mask | bit);
}

Note that this could also be reduced to the following by using the bitwise OR compound assignment operator:

private void SetBit(ushort bit, ref uint mask)
{
// OR
mask |= bit;
}

To toggle bits between zero and one, use eXclusive OR, also known as XOR. Exculsive OR looks at the two values to be XOR'd together bit by bit and asks, "Is only one of these bits set in each value?" If so, the resultant bit is set. Otherwise, it is cleared.

To clarify, consider the following:

Decimal 7 = Binary 0111
Decimal 2 = Binary 0010
Decimal 5 = Binary 0101

7 XOR 2 = 5

You should be able to see why from looking at the above binary representation. Here's how we'd do it in C#:


private void ToggleBits(uint key, ref uint value)
{
// XOR parameters together
value ^= key;
}

private void PerformOp()
{
uint Value = 7;
uint Key = 2;
ToggleBits(Key, ref Value);
}

Shifting Bits


To shift bits left and right, you can use the << (shift left) or >> (shift right) operators. As with all the other logical bitwise operators, you can also combine these with an equals sign to get a compound shift operator, e.g. <<= Shifting bits to the left has the effect of multiplying your original value by 2. Consider: Decimal 7 = Binary 00111 Shifting the bits left by 1 bit gives us 01110, which is, as expected, 7 multiplied by 2, which is 14. Shifting left again gives us 11100, which is 28, or 14 multiplied by 2. In C#:

private void ShiftLeft(ref uint value, ushort numberOfBitsToShift)
{
// Shift bits left
value <<= numberOfBitsToShift;
}

If you shift a set bit outside the range of your variable, the value is lost. For example, if I shift a whole byte set to 255 to the left, I end up with 254:

11111111 << 1 = 11111110

As shifting bits is less computationally expensive than multiplying values together, historically bit shifting is a common technique employed in assembly language when something has to run as fast as possible. Graphics rendering, for example.


Shifting right has a similar effect, but divides a number by 2. I won't post code for shifting right, I'm sure you get the idea by now. If not, you're in trouble!

I should probably also cover One's and Two's Complement. I'll do that in Part II.

Labels: ,

Bookmark and Share

Wednesday, 25 February 2009

Using the PropertyGrid Control in C#

If you're developing an application that requires object properties to be viewed and/or edited via a UI, the PropertyGrid can save you no end of time.

To demonstrate this, I'm going to create a Car class, declare an instance and assign it to a PropertyGrid.

First, here's my Car Class:



public class Car
{
public enum MakeEnum
{
AlfaRomeo,
AstonMartin,
BMW
}

[Description("Make of Car")]
public MakeEnum Make { get; set; }

[Description("Sticker Price")]
public int Price { get; set; }

[Description("Year of Manufacture")]
public int Year { get; set; }

[Browsable(false)]
[Description("Target Price")]
public int TargetPrice { get; set; }

[Description("Primary Car Colour")]
public Color Colour { get; set; }

[Description("Vehicle description")]
public string Description { get; set; }

[Browsable(false)]
public string DealerNotes { get; set; }

}

Notice the attributes set on the public properties. Firstly, the Description attribute's value is optionally shown in the bottom of the property grid when the property is selected.

The Browsable attribute allows the property to be flagged as invisible, so that it will not show up in a PropertyGrid. In this example, I don't want customers being able to see my target price or private dealer notes for a second hand car, so I've set Browsable to false.

Ok, so I have a form with a PropertyGrid on it, and a class called Car. I'm now going to instantiate a Car, and assign it to the PropertyGrid.


private Car CreateCar()
{
Car C = new Car()
{
Make = Car.MakeEnum.AlfaRomeo,
Colour = Color.Red,
Price = 3000,
Year = 1998,
TargetPrice = 2700,
Description = "One lady owner.",
DealerNotes = "Total wreck, offload ASAP!"
};

return C;
}

private void Form1_Load(object sender, EventArgs e)
{
Car C = CreateCar();
propertyGrid1.SelectedObject = C;
}

Pressing F5 and running the application produces a screen as follows:

Note that the Colour can be changed via the use of a ColorPicker, without any further coding being required. Likewise, the Car.Make property can be set by selecting from the pre-populated dropdown, which contains all the valid makes in MakeEnum.

Also notice that the DealerNotes and TargetPrice properties are hidden from view, and that our Description Attribute appears in the bottom section of the PropertyGrid.

It's also possible to create custom Property editors for use in a PropertyGrid by deriving a class from UITypeEditor. However, it's not quite as simple as that, and it a whole topic in itself. Part II, perhaps!

Labels:

Bookmark and Share

Tuesday, 24 February 2009

String, Byte[] and Char[] Conversion in C#

When I don't tend to work with particular types on a regular basis, the knowledge of how to manipulate them has the nasty habit of slowly trickling out of one ear. Over time, I'm left with only a faint inkling of how to do what I wanted. In particular, converting between strings, byte arrays and char arrays is something that just doesn't want to sink in with any kind of permanence.

So, if only for my own sanity, here's how to convert between strings, char arrays and byte arrays. The examples provided use UTF-8 Encoding, you should be careful to select the encoding that is relevant to your own code. For example,you might want to use the ASCIIEncoding class.

If you're not sure which encoding you need, I highly recommend you read this!

private static void DoConversions()
{

string MyString = "Meerkat";
char[] CharArray;
byte[] ByteArray;
string NewString;

UTF8Encoding Encoder = new UTF8Encoding();

// Convert String to Char Array
CharArray = MyString.ToCharArray();
Console.WriteLine(CharArray);

// Convert String to Byte Array
ByteArray = Encoder.GetBytes(MyString);
foreach (byte b in ByteArray)
{
Console.Write(b + ",");
}
Console.WriteLine();

// Convert Char Array to Byte Array
ByteArray = Encoder.GetBytes(CharArray);
foreach (byte b in ByteArray)
{
Console.Write(b + ",");
}
Console.WriteLine();

// Convert Byte Array to String
NewString = Encoder.GetString(ByteArray);
Console.WriteLine(NewString);

// Convert Char Array to String
NewString = new string(CharArray);
Console.WriteLine(NewString);

// Convert Byte Array to Char Array
CharArray = Encoder.GetChars(ByteArray);
Console.WriteLine(CharArray);

// Convert a subset of a string to a char array
CharArray = MyString.ToCharArray(4, 3);
Console.WriteLine(CharArray);
}


It's also worth mentioning that to convert between encodings you can use Encoding.Convert, which can be found in the System.Text namespace.

Labels:

Bookmark and Share

Saturday, 21 February 2009

Readability and Conditional Operators

Koush got me thinking about learning new things again.

Only relatively recently, I learnt how to use the C# conditional operator, which is '?', and how you can use it instead of an 'if' statement. In many cases, it's very useful. However, I'm wary of being clever for the sake of it. We all like new things, because they're shiny and new and when it's a coding technique, it makes us feel clever too. This can lead to the tendency to use techniques when, in reality, we probably shouldn't have done.

Consider the following two functions, both of which have the same result:


private bool IsValidHamsterQuantity(int hamsterQty)
{
if (hamsterQty > 7)
return false;

return true;
}

private bool IsValidHamsterQuantity(int hamsterQty)
{
return hamsterQty > 7 ? false : true;
}

In this case, my preference is the second option with the conditional operator because it's less lines of code, whilst still being easy to read.

However, this doesn't mean you should go replacing 'if' statements left, right and centre for the sake of using a conditional operator. Is it likely other developers will be maintaining your code? No doubt they'll marvel at your mastery of conditional operators, but they're not going to thank you in the readability stakes if you litter it with train wrecks like this:


private int GetData(DataRow DR, string column, int mod)
{
return (int)DR["Static"] == 1 ? (int)DR[column] + mod : ((int)DR["Var"] * (int)DR["Var2"]) + mod;
}

Yes, it is possible to squeeze a lot of code into one statement. But it doesn't always mean that you should.

Go forth, learn new things. And then, learn how to use them wisely.

Labels:

Bookmark and Share

Wednesday, 4 February 2009

Enum Flags Attribute

Even as an experienced programmer, it's nice to try and learn something new every day. In this case, I learnt that the Flags attribute can be used to make an enum into a bitmask.

For example:


[Flags]
public enum EntityFlags
{
NoEffect, // 0000
IsVisible, // 0001
IsSelectable, // 0010
IsCollidable, // 0100
IsDestroyable, // 1000
}

Developers can have a tendency to claim to know things that they don't, for fear of looking stupid, and maybe I've been guilty of that myself on occasion.

But these days, I'll be the first to put my hand up to say, "Yep, didn't know that!" and then proceed to ask more questions. It's good to ask questions. As one of the experts at TechED 2008 EMEA Developers said to me "There's no such thing as a stupid question."

Labels:

Bookmark and Share

Tuesday, 3 February 2009

Making the Most of IntelliSense

When using the Visual Studio IDE, we're blessed with the use of IntelliSense on all .NET assemblies. Type a class or method name, and not only will it offer to complete symbol names, but will offer brief documentation about the current selection via Reflection.

Most people know this. What I often see lacking however, is the documentation that goes with Intellisense when working with an in-house developed assembly. This is easy to add. Type three forward-slashes (as opposed to just two for a comment) above a class or method name, and Visual Studio will add in a summary header template. Within this header, you can describe your class or method and document the input and return parameters.

For example:



Here I've documented my method and the parameters it takes. Subsequently, when using IntelliSense to bring up this method, I also get my summary description:



When working on a large product with multiple developers, I often find thse summary comments to be invaluable. They're so simple to include - so do your fellow developers a favour and start including them!

Labels: ,

Bookmark and Share

Friday, 30 January 2009

Using the LinearGradientBrush for Good and not Evil

When it comes to Windows Forms applications, people these days have higher expectations about how they're going to look, especially with operating systems themselves coming with UI bells and whistles. When it comes to non-WPF C# apps, you can quite easily create something that looks like you dredged it out of the "My First VB6 Application" school of user interfaces if you're not careful.

Here's one way in which you can make good use of the LinearGradientBrush to add a nice gradient effect to a panel background:



Firstly, I'm going to seperate the actual drawing code into a method, so I can use it for other controls:



Then, in the paint event of the control I want the gradient to appear in:



Simple as that. Obviously, such an effect should be used judiciously - there are some particularly vile things that can be done with Gradient brushes I'm sure we've all seen examples of. But with a little patience, you can make your UI look that bit more appealing. You might also want to experiment with LinearBrush.SetBlendTrianglularShape and the values passed into the LinearBrush functions.

You can download the project file here:
GradientDemo.zip

Labels:

Bookmark and Share

Wednesday, 21 January 2009

To Dispose, or not to Dispose

The question was recently posed to me:
  • If the .NET framework handles garbage collection for me, why would I ever need to use object.Dispose()?
Well, although the framework may well handle the freeing up of memory automatically (which is great), you should really dispose of limited resources as soon as you're done with them. Database connections and GDI+ objects, for example.

Let's look at the GDI+ example in a bit more detail. Windows limits the number of GDI+ handles. So if you're creating a lot of GDI+ objects, for example, a number of Brush objects, the framework will dispose of them when the garbage collector gets around to it. In the meantime, you could have run out of available GDI+ handles (even though you haven't run out of memory). Unexpected Things may then occur.

As developers, we don't like Unexpected Things. In many cases, they may cause the people using your application to have cause for the client-helpdesk "just try rebooting it" scenario, which will miraculously fix the problem, but in the most horrible way.

So, in the case of our Brush it's better to do:


using (SolidBrush SB = new SolidBrush(Color.Red))
{
// Code
}


than:


SolidBrush SB = new SolidBrush(Color.Red);


The first way ensure that the Brush gets disposed ASAP, especially if the second example is used and Dispose() is not explicitly called.

Labels:

Bookmark and Share