March 29th, 2009 — 10:12 am
This is a fairly simple issue, but I had to rotate a drawn shape recently.
Initial searches led me to the TranslateTransform method, but I learned there is a simpler way to achieve this, using the RotateAt method of the Matrix class:
Graphics g = this.CreateGraphics(); //get a graphics object for the form
g.DrawRectangle(new Pen(Color.Black), 50, 50, 100, 100); //normal drawing
Matrix oldMatrix = g.Transform; //save the old Matrix object
Matrix myMatrix = new Matrix(); //create a new Matrix object
myMatrix.RotateAt(15, new PointF(200.0f, 200.0f)); //rotate the matrix in relation to a specific point (200, 200) by 15 degrees
g.Transform = myMatrix; //assign the new (rotated) matrix to the graphics object
g.DrawRectangle(new Pen(Color.Black), 100, 100, 100, 100); //perform the drawing
g.Transform = oldMatrix; //restore the original matrix
The result:

Comment » | Useful .Net classes
March 29th, 2008 — 01:04 pm
I had previously tried using the WebBrowser class to retrieve a web page for analysis (using the DocumentText property).
The problem was determining the state of the page load (since a page requires several hits before completion on average).
Turns out I need to check on track backs to my post, as Anthony Stevens (you should read his “about” page) found a solution to my problem:
Once the page load is complete WebBrowser.ReadyState property will be set to Complete, and then you can take the loaded page and wreak havoc on it.
Comment » | Useful .Net classes
March 22nd, 2008 — 08:24 am
As a .Net developer you are not bothered by trivialities such as character encoding, since the framework uses Unicode by default.
But what happens when you need to encode your text so someone else (non .Net) will decrypt it, and that someone uses a single byte per character?
Let’s start with few definitions:
-
ASCII - a standard that uses a single byte for each character, but only defines 128 possible symbols. There is no such thing as “Hebrew ASCII”.
-
ANSI - Same idea, but here you can use the remaining bits (out of a byte) to encode non-English specific characters. The problem is every language uses a specific version. The ANSI character table may look different on different computers, depending on the configuration.
-
Unicode, UTF-8, etc - Using 2 or more bytes for each character, allowing room for all languages (as long as both sides agree to use the same encoding)
(If you wish to learn more, you should read Joel Spolsky’s “The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)“)
Here are your options:
-
-
-
Encoding.UTF8 - Will encode ASCII text with a single-byte representation, but will switch to a longer representation for non-ASCII strings.
-
Encoding.Default - ANSI encoding based on the computer’s configuration, meaning both sender and receiver should share the same locale.
-
Encoding.GetEncoding - Uses a specific code-page to determine the desired encoding. You should try using this method if you need ANSI encoding. However, you still need to determine the code page you require.
-
Encoding.GetEncoding(862) - Uses
MS-DOS Hebrew encoding, with Hebrew characters starting at bit 128.
-
Encoding.GetEncoding(1255) - Uses
Windows-1255 Hebrew encoding, with Hebrew characters starting at bit 224. This encoding matches the
ISO 8859-8 standard, which is also used by
Unix.
Comment » | Useful .Net classes
March 9th, 2008 — 03:03 pm
This is a great way to create a Singleton instance of an exiting class without needing to specifically design it as a singleton:
// Singleton factory implementation
public static class Singleton where T : class
{
static Singleton()
{
// create the single instance of the type T using reflection
Instance = (T)Activator.CreateInstance(typeof(T), true);
}
public static T Instance { private set; get; }
}
class Program
{
public static void Main()
{
// test
Console.WriteLine(Object.ReferenceEquals(Singleton.Instance, Singleton.Instance));
}
}
1 comment » | Design Patterns, Useful .Net classes
March 4th, 2008 — 05:07 am
I have became aware recently to a performance problem with the Random class, specifically the Random.NextBytes method.
As it turns out this is an expansive operation:

The graph displays the number of milliseconds used to perform 10,000,000 operations.
The NextBytes method handle a byte array containing only 4 bytes.
This is linear - using an array of 4000 bytes will take 1000 times longer to complete the operation!
However, there may be a work-around using the BitConverter class.
Use BitConverter.GetBytes(Random.NextDouble()) to get a randomized array of 8 bytes.
Here are the results:

As you can see, generating an array of 8 bytes this way takes half the time of generating it using the NextBytes method.
Comment » | Useful .Net classes
March 3rd, 2008 — 02:16 pm
This is something I wrote a while ago to allow easy way to serialize/deserialize various objects to and from XML by creating an adapter around the XmlSerializer class.
Since I don’t know how this XML is going to be used this class stores it inside a string.
Serialize usage:
string s = SimpleSerializer.Instance.ToString(myObject);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(s);
Deserialize:
MyClass obj = SimpleSerializer.Instance.FromString(xDoc.OuterXML);
/// <summary> /// Objects serializer/deserializer /// </summary> public class SimpleSerializer { private static readonly SimpleSerializer instance = new SimpleSerializer(); //Singleton
/// <summary> /// Gets the class's instance. /// </summary> /// <value>The instance.</value> public static SimpleSerializer Instance { get { return instance; } }
/// <summary> /// Private constructor - prevents tempering with the singleton pattern /// </summary> private SimpleSerializer() { }
static SimpleSerializer() { }
/// <summary> /// Converts an object to it's XML represntation /// </summary> /// <param name="message">The source object</param> /// <returns>XML represntation</returns> public string ToString(object message) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(message.GetType()); using (System.IO.StringWriter sw = new System.IO.StringWriter()) { serializer.Serialize(sw, message); return sw.ToString(); } }
/// <summary> /// Converts an XML to an object /// </summary> /// <param name="message">The XML</param> /// <param name="type">The expected result class</param> /// <returns>A new object deserialized from the XML</returns> public object FromString(string message, Type type) { using (System.IO.StringReader sr = new System.IO.StringReader(message)) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type); return serializer.Deserialize(sr); } }
/// <summary> /// Converts an XML to an object /// </summary> /// <param name="message">The XML</param> /// <typeparam name="T">The expected result class</typeparam> /// <returns>A new object deserialized from the XML</returns> public T FromString<T>(string message) where T: new() { using (System.IO.StringReader sr = new System.IO.StringReader(message)) { System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); return (T)serializer.Deserialize(sr); } }
}
Comment » | Useful .Net classes, WCF
September 15th, 2007 — 09:10 am
Krzysztof Cwalina wrote a post detailing how have your class support the “foreach” loop without implementing the IEnumerable interface:
- Public GetEnumerator() method that returns a type with 2 members:
- The type will have a bool MoveMext() method.
- The type will have an Current property of type object.
Example:
class Foo { public Bar GetEnumerator() { return new Bar(); }
public struct Bar { public bool MoveNext() { return false; } public object Current { get { return null; } } }}
Comment » | Useful .Net classes