Category: .NET

How to fix the ArgumentException “Value does not fall within the expected range” when working with an image

Today I tried to reuse the image splitting feature in my program ShrinkerImages. Only with a single image, namely a photo from my Lumia 435. However, it was the first time I actually came across an image with a lower DPI than the usual 96. If an image has a DPI other than 96 (I …

Read More How to fix the ArgumentException “Value does not fall within the expected range” when working with an image

How to Add an Icon to a Console Application (C#)

Add references to these 4 assemblies to your console application: PresentationCore, System.Drawing, System.Xaml and WindowsBase. This is how you add the PresentationCore assembly, just press OK! The file(s) containing these classes must include the following using directives. They are the ones present in Program.cs after creating a console application, plus the ones I added via …

Read More How to Add an Icon to a Console Application (C#)

How to Remove All HTML Tags Using Regex

See how easily you can remove all formatting (HTML) from a text – just call this method: C#public static string StripTagsRegex(string source) { return Regex.Replace(source, "<.*/?>", string.Empty); } And how does it work? < and > are the boundary characters. . means any character, including digits, and * means that this character must occur at least 0 times. These …

Read More How to Remove All HTML Tags Using Regex

What the DateTime ToString() Methods Return

If your Windows is set to the USA style of date display, time, currency and numbers, the methods below will return the following: ToLongDateString(): Friday, July 27, 2012 ToLongTimeString(): 9:26:49 AM ToShortDateString(): 7/27/2012 ToShortTimeString(): 9:26 AM ToString(): 7/27/2012 9:26:49 AM If it is set to Czech / Czech Republic, you get this instead (the time …

Read More What the DateTime ToString() Methods Return

How to read XML with LINQ, or what to do when the .NET XML classes don’t do what you expect and return null

C#NameTable nameTable = new NameTable(); XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable); namespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom"); string c = @"F:\Mona\Visual Studio 2008\Projects\LinqLearn\LinqLearn\sunamoblog.wordpress.2012-03-12.xml"; XmlDocument xmlDocument = new XmlDocument(nameTable); xmlDocument.Load(c); xmlDocument.Save(c); You don’t have to worry about these namespaces getting duplicated, the XmlDocument class takes care of that. Now to LINQ itself. Using the XDocument.Element method, I managed to get an …

Read More How to read XML with LINQ, or what to do when the .NET XML classes don’t do what you expect and return null

Timers

The .NET BCL contains 3 timers: System.Timers.Timer – This is a timer in the sense of a stopwatch, meaning it tells you how long each operation took. It has methods for pausing and resuming. However, it can also work like a standard System.Windows.Forms.Timer; you just need to set the property System.Timers.Timer.AutoReset to true. When the …

Read More Timers