Fastest way to read dimensions from a picture / image file in C#

The fastest way to read the dimensions (width and height) from a picture / image file in C#, that I know of is:

DateTime startDateTime = DateTime.Now;
string sourceFolder = @"C:\Temp";
// Get all files from sourcefolder, including subfolders.
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
foreach (string file in sourceFiles)
{
using (Stream stream = File.OpenRead(file))
{
using (Image sourceImage = Image.FromStream(stream, false, false))
{
Console.WriteLine(sourceImage.Width);
Console.WriteLine(sourceImage.Height);
}
}
}
DateTime endDateTime = DateTime.Now;
Console.WriteLine(string.Format(
"Total duration [{0}] seconds. Total image count [{1}].", 
(endDateTime - startDateTime).TotalSeconds, 
sourceFiles.Length));

Total duration [0,1855235] seconds. Total image count [33].

 

This is like 250x faster, then:

DateTime startDateTime = DateTime.Now;
string sourceFolder = @"C:\Temp";
// Get all files from sourcefolder, including subfolders.
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
foreach (string file in sourceFiles)
{
using (Image sourceImage = Image.FromFile(file))
{
Console.WriteLine(sourceImage.Width);
Console.WriteLine(sourceImage.Height);
}
}
DateTime endDateTime = DateTime.Now;
Console.WriteLine(string.Format(
"Total duration [{0}] seconds. Total image count [{1}].", 
(endDateTime - startDateTime).TotalSeconds, 
sourceFiles.Length));

Total duration [47,4575263] seconds. Total image count [33].

How to add a CSS class to a HTML element, only when not present, by using jQuery.

You can add a CSS class to a HTML element, only when this class is not present on the HTML element, by using the jQuery function .toggleClass and using the switch parameter:

If your HTML page contains a div like:

<div id="title">
...
</div>

Adding the JavaScript code:

$(document).ready(function ()
{
// The CSS class "titleStyling", will now be added, because it is does not exist on the title yet.
$("#title").toggleClass("titleStyling", true);
// Subsequent calls will not add extra classes, where the jQuery .addClass will.
$("#title").toggleClass("titleStyling", true);
// Subsequent calls will not add extra classes, where the jQuery .addClass will.
$("#title").toggleClass("titleStyling", true);
});

Will result in

<div id="title" class="titleStyling">
...          
</div>

How to replace any .NET method with your own delegate, including non-virtual / static methods in sealed types.

So if you want to use TDD in .NET and use .NET types from for example mscorlib.dll, then you find your self creating interfaces for things like System.IO.File. System.DateTime etc., this is the way you do things in TDD, so the types can be mocked, but this is time consuming. To the rescue comes Microsoft Research with Moles (http://research.microsoft.com/en-us/projects/moles/).

 

This allows you to replace any .NET method with your own delegate, including non-virtual / static .NET methods in sealed types, in your test. So production code just uses the .NET types, but in your test you can mock these methods by your own implementations.

Watch the video at: http://channel9.msdn.com/blogs/peli/moles-replace-any-net-method-with-a-delegate

How to clear all canvases found in a div with jQuery

 

If you want to clear all canvases found in a given div with jQuery, you can use the following code:

    // Clear all canvases found in the given div, including all child canvases and sub child canvases.
$('#DivToFind canvas').each(function(idx, item) {
var context = item.getContext("2d");
context.clearRect(0, 0, item.width, item.height);
context.beginPath();        
});

MVC tip: use Url.Content in script src attribute, in *.cshtml files!

In your "*.cshtml" pages, use Url.Content and don’t directly specify the locations of the JavaScript files, because specifying the JavaSript files directly will result in 404 error, when using a virtual directory/application within a IIS website.

 

So use

<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
</head>

and don’t use:

<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>

How to create a stored procedure with T-SQL

Well for ages, I created mine stored procedures like:

if exists (select 1 from sys.objects where name = 'GetSales' and type = 'p')
begin
drop procedure [Reporting].[GetSales]
end
go
create procedure [Reporting].[GetSales]
@parameter1 int,
@parameter2 int
as
begin
select @parameter1, @parameter2    
end
go

, but there is a slightly shorter notation:

if object_id('[Reporting].[GetSales]') is not null
begin
drop procedure [Reporting].[GetSales]
end
go
create procedure [Reporting].[GetSales]
@parameter1 int,
@parameter2 int
as
begin
select @parameter1, @parameter2    
end
go

 

 

You’re never to old to learn Winking smile.